dashbitco/flow

Race condition when a producer is closing too soon

Fenntasy opened this issue · 3 comments

There's a weird behaviour when dealing with a producer that has too little elements to deal with:

** (exit) exited in: GenStage.close_stream(%{#Reference<0.212111494.3424387074.139905> => {:subscribed, #PID<0.672.0>, :transient, 500, 1000, 1000}, #Reference<0.212111494.3424387074.139907> => {:subscribed, #PID<0.674.0>, :transient, 500, 1000, 1000}, #Reference<0.212111494.3424387074.139908> => {:subscribed, #PID<0.675.0>, :transient, 500, 1000, 1000}})
         ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
     code: assert :ok = MyFlow.run()
     stacktrace:
       (gen_stage) lib/gen_stage/stream.ex:160: GenStage.Stream.close_stream/1
       (elixir) lib/stream.ex:1370: Stream.do_resource/5
       (elixir) lib/enum.ex:2979: Enum.reverse/1
       (elixir) lib/enum.ex:2611: Enum.to_list/1
       (flow) lib/flow.ex:1012: Flow.run/1
        my_flow_test.exs:46: (test)

Here is a gist that have minimal code for this bug: https://gist.github.com/Fenntasy/1e930da0f7b6c2055a660831d4406a96

You can toggle between lines 30 and 31 to provoke the error or to not have it occur.

When the list is only two element, the producer will send its :terminate event immediatly and the previous error will show up. Displaying something on the console (via IO.inspect) would also be enough to not have the error (presumably because it adds a little bit of time before the producer closes).

Am I doing something wrong or is it really a race condition?

Definitely a race condition. We were supposed to handle those scenarios but it seems we do not. Although it is a bug in GenStage and not Flow. :) No worries though, I will transfer it accordingly. Thank you!

I'm still having the issue. Has it been fixed yet in genstage?

@cblavier this PR was closed with a reference to a commit. So yes, it has been fixed (although it also required changes to Flow).