Repository navigation
Add port_in_use and exit_if_port_in_use to ls_zprocess - #140
Closed
chrisjbillington wants to merge 1 commit into
Closed
chrisjbillington wants to merge 1 commit into
chrisjbillington wants to merge 1 commit into
Conversation
This was referenced Oct 24, 2026
Merged
port_in_use(port) attempts a connection to 127.0.0.1 and closes it without sending anything. On Windows it disables SYN retransmission on the socket, so that a refused connection fails at once instead of after about a second, and makes the attempt without a socket timeout, which adds about 15 ms. exit_if_port_in_use(app_name, port, splash) logs and shows an error and exits if the port is in use, for the applications to call at start-up. On GitHub's Windows, macOS and Ubuntu runners, with Python 3.11 and 3.14, 200 calls each on a free port, a ZMQ server port and a plain listening socket all returned the right result. Windows calls took a median of 0.04 to 0.18 ms; Ubuntu and macOS took under 0.1 ms. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JHLgS3hh2FdpwGHFUTji9y
chrisjbillington
force-pushed
the
port-in-use
branch
from
October 24, 2026 02:03
0c71af0 to
e62f2e5
Compare
Member
Author
I'm closing this in favour of chrisjbillington/zprocess#41. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
If you start a second copy of BLACS, lyse, runmanager or runviewer while one is already running, the second copy fails with a
ZMQError: Address already in usetraceback when it binds its port. Depending on the application, it gets there only after doing a lot of start-up work. lyse has created its routine boxes by then, and a second BLACS (once blacs #138 is merged) would have created its device tabs and started their workers, which open the same hardware as the running copy.This PR adds two functions to
labscript_utils.ls_zprocess, so that the applications can check their port early in start-up:port_in_use(port)attempts a TCP connection to the port on 127.0.0.1 and closes it without sending anything. The ZMQ server on the other end never receives a message.exit_if_port_in_use(app_name, port, splash)logs an error and shows it in a message box saying that another copy is probably running, then exits.The Windows part needs some explanation. By default, Windows retries a connection to a localhost port that has no listener, so the attempt takes about a second to fail. A socket timeout avoids the wait but adds about 15 ms even when the connection succeeds.
port_in_usetherefore uses theSIO_TCP_INITIAL_RTOioctl through ctypes to turn off the retries on that socket, and makes the connection attempt without a timeout. With the retries off, Windows accepts or refuses a localhost connection at once.Testing:
port_in_useon its port and on a free port, and starts a second copy. Every call gave the right result, at most 1.0 ms on Windows. Every second copy reported that its port was in use within 1 to 3 s, and the second BLACS created no device tabs (run).馃 Generated with Claude Code
https://claude.ai/code/session_01JHLgS3hh2FdpwGHFUTji9y