Skip to content

Commit

Permalink
make handles not inheritable to prevent from blocking durning tab-com…
Browse files Browse the repository at this point in the history
…pletion (#852)

* make file handles not inheritable before spawn the daemon

Signed-off-by: Chen Lihui <[email protected]>
Co-authored-by: Chris Lalancette <[email protected]>
  • Loading branch information
Chen Lihui and clalancette authored Dec 21, 2023
1 parent 419afea commit d32d820
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions ros2cli/ros2cli/node/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import errno
import functools
import os
import platform
import socket

import rclpy
Expand Down Expand Up @@ -130,6 +132,22 @@ def spawn_daemon(args, timeout=None, debug=False):
return False
raise

# During tab completion on the ros2 tooling, we can get here and attempt to spawn a daemon.
# In that scenario, there may be open file descriptors that can prevent us from successfully
# daemonizing, and instead cause the terminal to hang. Here we mark all file handles except
# for 0, 1, 2, and the server socket as non-inheritable, which will cause daemonize() to close
# those file descriptors. See https://github.com/ros2/ros2cli/issues/851 for more details.
if platform.system() != 'Windows':
import resource
soft, _ = resource.getrlimit(resource.RLIMIT_NOFILE)
for i in range(3, soft):
try:
if i != server.socket.fileno() and os.get_inheritable(i):
os.set_inheritable(i, False)
except OSError:
# Just in case the file handles might be [3(closed), ..., 8(pipe handle), ...]
continue

# Transfer XMLRPC server to daemon (and the socket with it).
try:
tags = {
Expand Down

0 comments on commit d32d820

Please sign in to comment.