Skip to content

Instantly share code, notes, and snippets.

@evansd
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save evansd/e322d2457f5bea2be92e to your computer and use it in GitHub Desktop.

Select an option

Save evansd/e322d2457f5bea2be92e to your computer and use it in GitHub Desktop.
#!/bin/bash
# Work around bug in OpenSSH whereby the "ControlMaster" process that gets
# forked off fails to close stderr, causing Python's subprocess to hang forever
# waiting for EOF:
# https://bugzilla.mindrot.org/show_bug.cgi?id=1988
# Created named pipe to pass stderr through
stderr_pipe="$(mktemp -u)"
mkfifo "$stderr_pipe"
# Keep reading from stderr pipe and writing it to actual stderr
cat "$stderr_pipe" >&2 &
cat_pid="$!"
# Run SSH, sending its stderr to the named pipe
ssh "$@" 2>"$stderr_pipe"
status="$?"
rm "$stderr_pipe"
# Don't keep trying to read from stderr for more than 100ms after
# the main process has terminated
( sleep 0.1; kill "$cat_pid" 2>/dev/null ) &
# Wait for cat to finish, either because stderr has been closed or
# because the timeout has been reached
wait "$cat_pid" 2>/dev/null
exit "$status"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment