Can child processes be orphaned?


#1

I have some code which spawns a process when a struct is created and then waits for the process to finish when dropped. Is there a way for the child processes to be orphaned? Do I need to do anything to account for that or be aware of any concerns with that? I’ve dealt with this in other languages but I have no idea what the implications are in Rust.

I know in other languages you have to sometimes so something explicit to get them to close the child process with the parent. Is there anything like that in Rust? Is the child process guaranteed to end with the parent?


#2

There are some edge cases where Drop won’t be called for a struct. Some examples:

  • Another struct panics when dropped
  • Evil user calls mem::forget
  • Struct ends up trapped in a strong reference cycle

Depending on how bad an orphaned child would be, you will want to either ignore / explicitly document these edge cases, or use a destructor-less design that provides stronger guarantees of the child being awaited, usually at the cost of less pleasant ergonomics.


#3

Thanks!

The spawned process opens a window (using piston). I don’t think there are any negative consequences if the child process remains open. In the examples you gave, does the child process get orphaned because the parent process aborts before explicitly ending the child?


#4

It is not just about aborts, the specifics depend on circumstances. For example, in the case of mem::forget, an API user explicitly opts out of running the destructor of an object, which the stdlib considers safe, and the application continues to run normally as if nothing happened.


#5

So if I’m understanding this correctly, the process can only be orphaned if drop isn’t called (on Child) for whatever reason. Is that correct?


#6

Yes. If you wait for the child process on Drop, then that process can only be orphaned if drop isn’t executed.


#7

That makes sense. Thank you!