Libuv: child process stuck in defunct state if uv_close is called before child exited

Created on 5 Jul 2018  路  6Comments  路  Source: libuv/libuv

After libuv spawned a process using uv_spawn but before the process has exited you call uv_close, libuv will unregister the SIGCHLD handler for that child process and thus never call waitpid when the child exits and cause the child to forever be stuck in the defunct state.

Ether libuv should be updated to internally keep track of this after uv_close has been called or the documentation should be updated to state not to call uv_close until the exit callback has been called.

  • Version: 1.19.1
  • Platform: Linux 3.10.0-693.5.2.el7.x86_64 #1 SMP Fri Oct 20 20:32:50 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

Spawn logic:

      uv_process_options_t options;
      memset(&options, 0, sizeof(uv_process_options_t));

      options.exit_cb = onExit;
      options.file = exe.c_str();
      options.cwd = workingDir.data();
      options.flags = UV_PROCESS_DETACHED;
      options.args = (char**)&argsPtr[0];

      uv_spawn(&loop, m_handle, &options);

      // Need to release the handle to the process due to UV_PROCESS_DETACHED
      uv_unref((uv_handle_t*)m_handle);

Then clean up logic before child has exited that causes it to get stuck:

      uv_close_cb onClose = [](uv_handle_t* handle) 
      { 
        DELETE((uv_process_t*)handle);  
      };    

      uv_close((uv_handle_t*)m_handle, onClose);    
      m_handle = nullptr;
doc good first issue help wanted

Most helpful comment

It does seem like it would be good to document this, since it could be a surprising gotcha (e.g. it took us a while to notice and fix this in JuliaLang). What's the thinking on keeping "doc" issues open (and "help wanted / good first issue" at that)?

All 6 comments

That's a documentation issue. Pull request welcome.

I would like to work on this issue

@nimit95 please open a PR if you're still interested in tackling this.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

It does seem like it would be good to document this, since it could be a surprising gotcha (e.g. it took us a while to notice and fix this in JuliaLang). What's the thinking on keeping "doc" issues open (and "help wanted / good first issue" at that)?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fliser picture fliser  路  7Comments

justinmk picture justinmk  路  5Comments

rynz picture rynz  路  3Comments

cjihrig picture cjihrig  路  4Comments

zbjornson picture zbjornson  路  9Comments