$fopen usage leads to 'Too many open files' error

Running a simulation with many rstb toggles seems to eventually hit a ‘Too many open files’ error as a result of the following code.

Shouldn’t the $fopen happen only once at the beginning of the simulation, i.e., on the very first rstb release? The remainder of the simulation should be stuck in the forever loop until the simulation ends, at which point the file should close. Is there any chance that subsequent resets will cause further $fopen’s? Or is this a tool issue?

int my_file;
initial begin
    @(negedge rstb)
    my_file = $fopen("./my.log"), "w"); // always overwrite at beginning of sim
    if (my_file == 0) begin
        $display("file open failed");
    end

    forever begin
    @(posedge clk);
        if (valid) begin
            $fdisplay(my_file, "some info");
        end
    end
end

final begin
    $fclose(my_file);
end

In reply to atashinchi:

This could be a OS platform issue or a tool issue, assuming there is only one instance of the module that has this code.

Can you try running a simpler example the opens up a file at time 0?

Thanks I can try that.

Actually I should note that there are indeed multiple modules with this code. Each module instance will create its own uniquely-named file on

negedge rstb

. The number of modules depends on the test but could get up to something like 64. A quick google search suggests the limit on $fopen is 32 at a time. Is this correct?

In reply to atashinchi:

When using 2 arguments to $fopen, the limit is set by the platform the tool is running on. You should find out what that limit is.