Skip to content

Commit

Permalink
Robustify nbdkit startup: always wait for sockpath to be created
Browse files Browse the repository at this point in the history
Signed-off-by: Ronan Abhamon <ronan.abhamon@vates.tech>
  • Loading branch information
Wescoeur committed Nov 20, 2024
1 parent 4c419e6 commit 58437a2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
44 changes: 42 additions & 2 deletions nbd_http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import signal
import subprocess
import sys
import threading

WORKING_DIR = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), '')
NBDKIT_PLUGIN = WORKING_DIR + '../lib64/nbdkit/plugins/nbdkit-multi-http-plugin.so'
Expand Down Expand Up @@ -65,8 +66,14 @@ def run_or_ignore(fun):

# -----------------------------------------------------------------------------

THREAD_PRINT_LOCK = threading.Lock()

def thread_print(str):
with THREAD_PRINT_LOCK:
print(str)

def eprint(str):
print(OUTPUT_PREFIX + str, file=sys.stderr)
thread_print(OUTPUT_PREFIX + str)

# -----------------------------------------------------------------------------

Expand Down Expand Up @@ -242,7 +249,39 @@ def clean_paths():
]
if device_size is not None:
arguments.append('device-size=' + str(device_size))
server = subprocess.Popen(arguments)

# Start nbdkit process.
server = subprocess.Popen(
arguments,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
env=dict(os.environ, PYTHONUNBUFFERED='1')
)

# Wait for init.
try:
with timeout(10):
while server.poll() is None:
line = server.stdout.readline().rstrip('\n')
if not line:
continue
print(line)
if 'written pidfile' in line:
break
except Exception as e:
raise Exception('Failed to start nbdkit server: {}.'.format(e))

# Continue to log server messages in stdout.
def log_server_messages():
while server.poll() is None:
line = server.stdout.readline().rstrip('\n')
if line:
thread_print(line)

server_stdout_thread = threading.Thread(target=log_server_messages)
server_stdout_thread.start()

nbd = None

try:
Expand All @@ -261,6 +300,7 @@ def clean_paths():
nbd.disconnect()
server.send_signal(signal.SIGQUIT)
server.wait()
server_stdout_thread.join()
clean_paths()

# ==============================================================================
Expand Down
6 changes: 4 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ def start_http_server(backing_path):
arguments,
stdout=sys.stdout,
stderr=subprocess.STDOUT,
preexec_fn=os.setsid
preexec_fn=os.setsid,
env=dict(os.environ, PYTHONUNBUFFERED='1')
)

# ------------------------------------------------------------------------------
Expand All @@ -160,7 +161,8 @@ def start_nbd_server(volume_name, device_size):
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
preexec_fn=os.setsid,
universal_newlines=True
universal_newlines=True,
env=dict(os.environ, PYTHONUNBUFFERED='1')
)

try:
Expand Down

0 comments on commit 58437a2

Please sign in to comment.