Skip to content

Two session launches racing for a stale socket can both claim it #18962

Closed

Description

@kevinushey

Summary

claimLocalStream() (src/cpp/core/include/core/http/LocalStreamSocketUtils.hpp, added in #18944) binds first. If the bind fails with EADDRINUSE, it checks whether anything is listening on the path with a non-blocking connect(), and if nothing answers, it removes the path and binds again. Two processes claiming the same stale path at the same time can both get past that check:

  1. A and B both fail to bind with EADDRINUSE, and both find nothing listening.
  2. B removes the stale socket, binds, and listens.
  3. A's removeIfExists() unlinks B's new socket, and A binds.

B keeps running, listening on an unlinked socket, which is the state that locked the user out in #18941. B also reads its socket's identity with lstat() on the path after binding, so if A rebinds between B's bind and B's lstat(), B records A's identity and removes A's socket when it exits.

The comment "a process that bound the path since our probe keeps it" covers a bind that lands between A's remove and A's rebind, but not one between A's probe and A's remove.

Likelihood

This needs a stale socket (left by a session that died without cleaning up) and two launches of the same session reaching the claim within microseconds of each other. #18944 closed the much wider window (seconds) behind #18941, so this is hardening rather than an observed failure.

Possible fix

Serialize the check, remove, and rebind with an exclusive flock() on a lock file next to the socket (e.g. <stream>.lock), held only for the duration of the claim. Once the claim is serialized, reading the identity from the path with lstat() is safe as well.

Related: #18941, #18944.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions