-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
In the MessageBuffer
, detect if we've just been called by a fork child.
#199
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a few notes!
One thing that occurred to me is that another possible approach would be to move the fork checking to the Sender implementaiton. Doing so would allow a trivial optimization -- the default Sender
can avoid all of this checking, because naturally its inner thread dies on fork
, and so, while that thread lives, we're sure that no fork
happened.
So only the SingleThreadSender
would need to have the fork-checking behavior.
Also -- probably useful to add some tests and a microbenchmark to validate that the cost of calling Process.pid
is reasonable :)
@ivoanjo Please correct me if I'm wrong in my thinking but I think that what you are saying isn't correct: in both mode (with or without companion thread), we have to detect that we are now running in a different PID, because it means that we are in a new process (it can happen if an user is forking directly in their code or within a framework). Because of that fork, we have to clean the buffer of its existing data in the new process, to avoid having both processes sending the same data which was originally in the buffer the moment the fork happened. Does it make sense? |
Yup!
Not necessarily -- which was what prompted my suggestion (but it was more of a "may be interesting" and not a "I really think this needs to change" suggestion). In the current approach in this PR, the fork detection has been added to the What I was saying is, that in the [1] pry(main)> background_thread = Thread.new { sleep }
=> #<Thread:0x00007fc41a84c430 (pry):1 sleep>
[2] pry(main)> fork do
[2] pry(main)* puts "background thread status: #{background_thread.inspect}"
[2] pry(main)* end
background thread status: #<Thread:0x00007fc41a84c430 (pry):1 dead>
=> 22780
[3] pry(main)> background_thread.inspect
=> "#<Thread:0x00007fc41a84c430 (pry):1 sleep>"
[4] pry(main)> So an alternative in the regular But, as I said above, in the |
Got it, thanks for the detailed explanation. Indeed performance-wise that may be interesting to do that instead in the original |
Replaced by #203. |
In order to clean the
MessageBuffer
in this case, because it can contains data from the parent process instead.