Skip to content

Feat/bwrap private netns - #37

Merged
Matt Snyder (maatthias) merged 39 commits into
NSLS2:mainfrom
tacaswell:feat/bwrap-private-netns
Nov 19, 2026
Merged

Matt Snyder (maatthias) merged 39 commits into
NSLS2:mainfrom
tacaswell:feat/bwrap-private-netns

Conversation

@tacaswell

Copy link
Copy Markdown
Contributor

Summary

This is a draft of starting to add network controls to the sandbox. Initially we took the position "the agents need the network, guess we have to allow it", but there are a number of steps we can take short of full network cut off.

Description

  • opt-in to allowing anything in the sandbox to expose a port to localhost (port conflicts handled by leveraging the full 127.0.0.0/8 subnet)
  • opt-in allowing port from the host localhost to be exposed to things in the agent
  • escape hatch to go back to sharing the network with the host
  • supports to backends to manage this (to go back to RHEL8)
  • adds a way to log the network traffic to see what it is talking to (this is to maybe start building up a plan to be more aggressive and have an allow-list of dns lookups things in the sandbox can do)

Taken together these will keep LSP or opencode2's servers from talking to each other from inside of the sandbox but allows for e.g. letting the agent run a dev server you poke from a browser.

Testing

Not much yet, have not tried running with this as my local wrappers in anger yet.

Checklist

  • Edit the sections above with relevant information
  • Update in-repo documentation, if applicable
  • Run pixi run -e dev lint locally (or wait for CI) — see .github/workflows/lint.yml
  • Test the changes on a representative managed host (or runansible deployment)

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Blocking writes to a stalled logging subprocess can prevent pasta logs from being drained and stall the sandbox’s network.

3 open issues
What changed in this PR

This PR adds private-network controls to the bw* sandbox wrappers, separating agent services from host loopback services while retaining outbound access and explicit port forwarding.

Changes:

  • Integrates private networking, port publication, host-port forwarding, and a shared-network escape hatch across all five wrappers.
  • Adds network-log processing, a DNS relay, and sandboxed OpenCode 2 web serving with password authentication and pairing links.
  • Adds tests, fake network backends, documentation, and CI integration.
File Description
tests/​test_serve_timeout.py Tests stalled pairing-request timeout handling.
tests/​test_netlog.py Tests log condensation, DNS parsing, and relay behavior.
tests/​fakes/​pasta-bin/​pasta Simulates pasta networking and flow logs.
tests/​fakes/​bin/​unshare Simulates namespace setup and failures.
tests/​fakes/​bin/​systemd-cat Captures journal output for testing.
tests/​fakes/​bin/​slirp4netns Simulates slirp setup and port forwarding.
tests/​fakes/​bin/​logger Captures syslog output for testing.
tests/​fakes/​_nsproxy.py Provides namespace socket and forwarding helpers.
tests/​fake-net-run Runs wrappers with fake network backends.
tests/​data/​pasta-debug.log Supplies a realistic log-parser fixture.
README.md Documents network dependencies and common options.
pixi.toml Adds actionlint and a unit-test task.
pixi.lock Locks actionlint packages across supported platforms.
lib/​bwrap_netlog.py Implements flow-log condensation and DNS logging.
docs/​bw-wrappers.md Documents networking, logging, descriptor relay, and web serving.
bin/​bwopencode2 Adds network options, sandboxed web serving, and prek cache mounting.
bin/​bwopencode Integrates network controls and prek cache mounting.
bin/​bwcopilot Integrates networking, descriptor relay, and prek cache mounting.
bin/​bwcodex Integrates networking and descriptor-based configuration copies.
bin/​bwclaude Integrates networking and descriptor-based credential copies.
AI_DISCLOSURE.md Updates model disclosure and its date.
.pre-commit-config.yaml Runs actionlint through the pixi environment.
.github/​workflows/​lint.yml Adds unit tests to CI.
.codespell.ignore Allows the passt package name.

Review effort: Balanced

Comment thread lib/bwrap_netlog.py Resolved
Comment thread lib/bwrap_netlog.py Resolved
Comment thread tests/fake-net-run Resolved
Runs opencode2 serve inside the sandbox on 127.0.0.1, so tools stay sandboxed, with one server per invocation on the first free port of a fixed range (24700-24709 by default) so one ssh_config block covers every session. The password is random for each session unless BWOPENCODE2_SERVE_PASSWORD is set, and reaches the server only as OPENCODE_PASSWORD. Ports are claimed with a host-side file so two launchers cannot pick the same one. --serve-ssh-config prints the matching ssh_config block.

Assisted-by: opencode:claude-sonnet-5-5
The web UI opens on a page that asks for a pairing link or the password, and opencode pair points at the shared service, which the wrapper refuses. Inside the sandbox a small wrapper now starts the server, waits for it, asks it for a pairing code and prints the link, so the browser can sign in without typing the password. The password reaches curl on stdin, never on a command line. Use password still works after the link expires.

Assisted-by: opencode:claude-sonnet-5-5
pasta, which will start bwrap in a private network namespace, closes
every inherited descriptor above 2, so the --file and --ro-bind-data
descriptors opened by the launcher would never reach bwrap. The
launcher now only reserves descriptor numbers and records their source
(a host file, or generated text carried in the environment); a fixed
bash relay opens them at those numbers and execs bwrap.

bwcodex no longer stages temporary copies of its config and daemon
settings, and --dry-run shows real descriptor numbers instead of
placeholders.

Assisted-by: opencode:claude-opus-5-5
pasta (package passt) now starts each sandbox in its own network
namespace. Outbound connections still leave from the host's address,
but the host's loopback services are unreachable and the sandbox's
listeners are invisible to the host. There is no fallback to the host
network: without a working pasta the launchers exit 3.

Crossing the boundary is a per-port, per-launch opt-in:
- --publish PORT[:SANDBOXPORT] binds a per-user, per-session host
  address 127.H1.H2.S (--publish-addr overrides it, e.g. for OAuth);
- --host-port PORT[:SANDBOXPORT] makes the host's 127.0.0.1:PORT
  reachable as host.internal, with a deny list (CUPS, Docker, X11,
  Tailscale, the OpenCode 2 service);
- --net-log FILE records every flow.

pasta maps the caller to uid 0 in its user namespace, so bwrap now runs
the agent as the real uid in a nested one (--unshare-user --uid --gid);
without it the agent would be root with a full capability set there.
bwrap also restores the real hostname that pasta replaces.
/etc/resolv.conf and /etc/hosts are generated, the context file gains
a Network section, and BWRAP_NET_* describe the forwards.

This breaks --serve, will fix later.

Assisted-by: opencode:claude-opus-5-5
Document the pasta requirement, --publish / --publish-addr /
--host-port / --net-log, the per-user publish addresses and what they
do not guarantee, host.internal and the loopback-trust caveat, and the
descriptor relay.

Assisted-by: opencode:claude-opus-5-5
passt is in RHEL 9 but not RHEL 8. Without a working pasta the
sandbox now shares the host network as before, with a warning, instead
of refusing to start. --local-net chooses the host network explicitly
and silences the warning. The forwarding options still need pasta:
with --local-net they are an error, and without pasta they stop the
launch (exit 3). BWRAP_NET_MODE tells the sandbox which mode it is in,
and the context file describes both.

Assisted-by: opencode:claude-opus-5-5
Use bwrap's own term for sharing the host network namespace.

Assisted-by: opencode:claude-opus-5-5
Without a usable private network namespace the launchers now exit 3
naming the reason, instead of warning and sharing the host network.
Only --share-net shares it. BWRAP_NET_BACKEND (pasta or none) replaces
BWRAP_NET_MODE, ready for a second backend.

Assisted-by: opencode:claude-opus-5-5
RHEL 8 has slirp4netns but no passt. When pasta is not usable, the
launchers now use slirp4netns: util-linux unshare gives the launcher a
user namespace that owns a new network namespace, bwrap runs the
sandbox in it as the real user, and a background helper attaches
slirp4netns (--disable-host-loopback, 192.168.254.0/24, built-in DNS)
before releasing the sandbox through --block-fd. The launcher still
execs in the foreground; slirp4netns exits with bwrap (--sync-fd). On a
setup failure the helper kills the blocked sandbox before the tool
starts.

unshare, not bwrap --unshare-net, makes the namespaces: bwrap's devpts
setup nests the sandbox in a second user namespace, from which
slirp4netns would have no rights over the network namespace.

--publish works through the slirp4netns API socket; --host-port and
--net-log need pasta. --net-backend restricts the choice to one
backend.

Assisted-by: opencode:claude-opus-5-5
Document the selection order (pasta, slirp4netns, else stop unless
--share-net), --net-backend, what each backend supports, and how the
slirp4netns launch works.

Assisted-by: opencode:claude-opus-5-5
--serve now adds its port to --publish: the server binds 0.0.0.0 inside
the private namespace and is reached on the host at the user's publish
address 127.H1.H2.S (normally .1), with both backends. With --share-net
it binds 127.H1.H2.1 itself. The port is chosen free on that address,
the printed ssh -L command and pairing links name it, and
--serve-ssh-config forwards to it. net_publish_prefix exposes the
per-user prefix.

Assisted-by: opencode:claude-opus-5-5
The slirp4netns pipes and API socket were in a mktemp directory under
XDG_RUNTIME_DIR or TMPDIR, whose parent was not checked: another user
able to write it could swap the directory during the launch. They now
go in a per-launch session directory under $XDG_RUNTIME_DIR/bwrap-net,
else /tmp/bwrap-net-UID, used only when no other user can change its
parent and it is the user's own 0700 directory. TMPDIR is never used.

Both locations are blocked paths, so no sandbox of the user can mount
them and reach another sandbox's API socket.

Assisted-by: opencode:claude-opus-5-5
The slirp4netns helper kept polling after the launcher had gone (for
example when unshare failed) and then sent SIGKILL to the saved pid,
which by then could belong to another process. It now exits quietly as
soon as its parent is no longer the launcher, and before killing it
checks that the launcher is still its parent and that the sandbox pid
is the launcher's child.

Assisted-by: opencode:claude-opus-5-5
The publish address came from one ss snapshot, and the backend binds
it only after exec, so two launches at the same moment could pick the
same address and port. The choice is now made under flock in the
runtime directory, and each launch leaves a claim (launcher pid and
start time, host ports) that later launches respect; claims whose
process is gone are removed. --serve port claims also record the start
time, so a reused pid does not keep them alive.

Assisted-by: opencode:claude-opus-5-5
The header still said that without pasta the sandbox shares the host
network with a warning; the launch tries slirp4netns and otherwise
stops unless --share-net is given.

Assisted-by: opencode:claude-opus-5-5
pasta writes one log file, and the network log becomes an always-on
journal record in the next commit; a file the user names is easier to
edit, lose, or leave where the agent can write it.

Assisted-by: opencode:claude-opus-5-5
…m log

Every launch writes start and end records (tag bwrap-net, a session
id also given to the sandbox as BWRAP_SESSION) with systemd-cat, else
logger; with neither it warns and runs without the log. There is no
option to turn it off.

With pasta every flow is logged too: pasta runs with --debug and a
FIFO as --log-file (huge --log-size, as its rotation fails on a FIFO)
and as its stderr, and a supervisor outside the sandbox copies each
line to the log, holding the FIFO open so pasta never blocks. The
descriptor relay prints a marker and reopens the user's stderr, so
pasta's setup lines go to the log, and if pasta fails before the
marker its lines are shown on the terminal.

Assisted-by: opencode:claude-opus-5-5
tests/fake-net-run runs a launcher against fake pasta, slirp4netns,
unshare, systemd-cat and logger, where /dev/net/tun is missing (as in
a development sandbox). The fakes really create or join the namespaces
and forward TCP, so --publish, --serve and the network log can be
tested end to end; failure modes are read from $FAKE_NET_DIR.

Assisted-by: opencode:claude-opus-5-5
A --serve session could move to another session index when one of its
ports was busy on 127.H1.H2.1, and the --serve-ssh-config alias then
did not reach it. --serve now fixes the session's publish address to
.1: its port is chosen free and unclaimed there, a clash with another
--publish port stops the launch with the reason, and --publish-addr is
refused with --serve.

The fake pasta now handles comma-separated port lists and never starts
the command when its setup failed.

Assisted-by: opencode:claude-opus-5-5
pasta's -T takes no listening address ("Listening address not allowed
for -T"): it always listens on the namespace's loopback. So
--host-port PORT[:SANDBOXPORT] now passes -T SANDBOXPORT:PORT and the
agent reaches the host service at 127.0.0.1:SANDBOXPORT; host.internal,
127.0.42.1 and BWRAP_NET_HOST_ADDR are gone. That sandbox port is held
by pasta, so a --publish to it is refused, and the context file says
the agent cannot serve on it.

The fake pasta now rejects an address on -T/-U and listens on the
namespace's loopback, as the real one does; it had accepted the old
form, which is why the tests passed.

Assisted-by: opencode:claude-opus-5-5
pasta's debug log has about 34 lines per connection (state changes,
timers, ACK bookkeeping) plus a doubled setup dump, and only addresses.
lib/bwrap_netlog.py condense now turns it into one logfmt line when a
connection opens and one when it closes (with its duration), a fail
line for connections that never became active, and pasta's warnings
and errors; 5000 lines of a real session became 280.

To name destinations, bwrap_netlog.py dns runs as a DNS relay on
127.53.0.1:53 in the sandbox's network namespace, started by the
descriptor relay before bwrap (it waits until the relay listens, at
most 0.25 s) and dropping its capabilities after binding. It logs one
dns line per lookup and forwards to pasta's DNS address, which
resolv.conf lists second so lookups go on working if the relay is
down. The condenser adds name= to connections from the answers. Both
need Python 3.6+ (platform-python on RHEL 8); without it the log is
unfiltered as before.

Assisted-by: opencode:claude-opus-5-5
Unit tests (pixi run -e dev test) against part of a real pasta debug
log with documentation addresses, and a DNS relay against a local fake
upstream. The fake pasta now writes pasta's debug format for its
forwards and answers DNS on --dns-forward, so the launchers' log can
be checked end to end with tests/fake-net-run.

Assisted-by: opencode:claude-opus-5-5
On a real session the first IPv6 connections were logged without a
name: the client connected as soon as it had the answer, before the
relay had written its dns line. The relay now logs first. CNAMEs go
to cname= so answers= holds addresses only.

Assisted-by: opencode:claude-opus-5-5
…rk log

name= did not say what it was. Connection and dns lines now use
domain= (the domain the relay looked up), which is also what an
allow-list is written in.

Assisted-by: opencode:claude-opus-5-5

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stuwilkins

Copy link
Copy Markdown
Contributor

Pushed one commit to this branch: feat(bwrap): name the user on every network log record.

Until now only the start and end records had user=, so a dns, open, close or fail line could be attributed only by finding its session's start record. Every record now begins session=ID user=NAME:

session=3493048fc341 user=alice dns domain=github.com type=A ...
session=3493048fc341 user=alice flow=7 open proto=tcp dir=out ...
  • The condenser takes a new --user NAME. The fallback used without Python writes the same prefix.
  • NAME is the account of the launcher process (id -un), not $USER, which the caller can set. Characters outside A-Za-z0-9._@- become _, so it cannot split or forge a logfmt field. The start and end records use the same value.
  • The log still cannot say which process inside the sandbox made a request, only who started the sandbox.
  • docs/bw-wrappers.md is updated, including the journalctl examples (-g 'user=NAME dns domain=' now works in one step). Tests added in tests/test_netlog.py; the Python suite and the repository's hooks pass.
  • Sessions logged before this change still have user= on their start and end records only.

Assisted-by: opencode:claude-sonnet-5-5

@stuwilkins

Copy link
Copy Markdown
Contributor

Pushed one commit to this branch: fix(bwrap): address the Copilot review of the network log. Copilot's last review had six open findings; this fixes all of them, and the fixes were reviewed by four models before the push, which found nothing serious and a few small things that are also fixed.

  • Stalled log tool (the high one): condense kept blocking in write() when systemd-cat/logger stayed alive but stopped reading, which stops the FIFO being drained and stalls the sandbox's network. Records now go through a queue bounded in bytes (8 MiB) and a writer thread. When it is full they are dropped and counted, the next record carries a condense level=warn msg=log-tool-behind dropped=N note, and at the end the tool is killed if it has not taken what is queued. A healthy tool gets every record, also under a burst (tested with 50,000). Without Python the bash fallback still writes synchronously; the docs now say so.
  • Piped stderr bridge: forwards bytes as they arrive, unchanged.
  • via=: also recorded when only the port differs (--host-port 5432:15432).
  • TCP DNS: a complete query that fails upstream is logged once as rcode=TIMEOUT; clients that close early or send less than a DNS header are not.
  • tests/fake-net-run: a custom FAKE_NET_DIR is bound at the fixed fallback path.

New tests are in tests/test_netlog.py (the whole suite: 69 tests, run repeatedly and on one core). The hooks pass. Not tested on a live pasta or on RHEL 8's Python 3.6; the code avoids anything newer, but I could not run it there. AI_DISCLOSURE.md lists the three review models that were added.

Assisted-by: opencode:claude-sonnet-5-5
Assisted-by: opencode:gpt-6-astra
Assisted-by: opencode:gpt-6.1-sol
Assisted-by: opencode:claude-fable-5-2
Assisted-by: opencode:claude-opus-5-5

Copilot AI balanced review requested due to automatic review settings November 16, 2026 13:45

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because the pasta handoff can release the shared-database lock while OpenCode is still running without a working network logger, risking concurrent writes.

No open issues 🎉

6 resolved since last review

Review effort: Balanced

@tacaswell

Copy link
Copy Markdown
Contributor Author

Stuart B. Wilkins (@stuwilkins) rebased and forced push so that the system will let you approve and merge.

@stuwilkins

Copy link
Copy Markdown
Contributor

Pushed one commit to this branch: fix(bwrap): keep the database lock held under pasta without a log tool. It is option 2 from my comment on 11 Nov (a holder process), so that the guard keeps working while we decide whether to pass the lock down the descriptor relay later.

The problem. bwopencode2's database guard takes a shared flock on a descriptor of the launcher. A flock lasts only while some process holds the open descriptor. With --share-net the final exec passes it to bwrap and OpenCode. With a private network the exec goes into pasta, which closes every inherited descriptor above 2, so the lock survived only because the network log helper, forked just before the exec, happened to hold a copy. With no working log tool, or if the helper died, the lock was released while OpenCode ran, and another host or OpenCode build could pass the guard and open the NFS database.

The fix.

  • net_hold_fd FD registers a descriptor; _net_hold_start (called in launch_sandbox just before the exec, for pasta and slirp4netns) starts a background holder that keeps its own copy open until the launcher process (pasta, after the exec) has gone.
  • The holder runs on the host, ignores the session's signals, closes every other descriptor it inherited (slirp4netns exits only when bwrap closes its sync descriptor), and waits with /usr/bin/sleep, not one found through PATH: a review pointed out that PATH can include a directory the sandbox can write to, and the holder runs outside it.
  • bwopencode2 registers the guard's descriptor. --share-net is unchanged.
  • bwopenchamber (feat(bwopenchamber): add bwopenchamber and bwopencode2 --serve-open #43) needs the same one-line registration; I will add it there.

Tests. tests/test_net_hold.py: a stand-in that closes descriptors from 3 up as pasta does, checked from outside with flock. A control without the holder loses the lock; with it the lock stays held while the sandbox runs, is released soon after, the holder keeps no other descriptor, ignores INT/HUP/TERM/QUIT, and nothing starts under --share-net. Each check fails if the holder's descriptor closing or signal handling is removed. 77 Python tests and the 80 guard checks pass; the hooks pass.

Not tested. A real launch: the nested sandbox where I work refuses /usr/bin/bwrap. During a session on a real host, fuser on the lock file in ~/.local/share/opencode2/opencode-db-guard should now list the holder as well as the log condenser.

What it does not fix. The lock is held next to OpenCode, not by it, and the holder watches the launcher, not the sandbox: if the holder is killed the lock goes while OpenCode runs, and if the launcher is killed first the lock goes at once. Passing the lock down the relay (option 1) would remove both; docs/bw-wrappers.md and docs/opencode2-db.md now say so.

Assisted-by: opencode:claude-sonnet-5-5
Assisted-by: opencode:gpt-6-astra

Copilot AI balanced review requested due to automatic review settings November 16, 2026 17:28

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Concurrent --serve launches can reserve the same port, and unescaped passwords containing quotes or backslashes can break /api/pair authentication.

No open issues 🎉

6 resolved since last review

Review effort: Balanced

@stuwilkins

Copy link
Copy Markdown
Contributor

Copilot's last review of this PR names two problems in the --serve code in its headline, with no inline comment: concurrent --serve launches reserving the same port, and a password with a quote or backslash breaking the /api/pair request. Both are in code from the first two commits here (bwopencode2 --serve), and both are handled in #43, which is stacked on this PR, so they are fixed once the two merge. I checked each against the code:

  • Port race: real. serve_claim_port checks a claim file, removes it if stale, and creates its own with noclobber. Two launches that start together and see the same stale file can each remove the other's new one and end up with the same port. feat(bwopenchamber): add bwopenchamber and bwopencode2 --serve-open #43 holds an flock on ~/.local/state/bwopencode2/serve/claims.lock while stale claims are cleaned and a port is chosen, and releases it before the sandbox starts. A test starts six launches at once against stale files; without the lock it fails on every run.
  • Password: already handled in feat(bwopenchamber): add bwopenchamber and bwopencode2 --serve-open #43, which escapes a backslash and a double quote before writing curl's config (the wrapper's pair() and the host-side serve_request_code). This PR still has the older wrapper without the escaping. feat(bwopenchamber): add bwopenchamber and bwopencode2 --serve-open #43 also adds a test for the host-side path, and refuses a BWOPENCODE2_SERVE_PASSWORD with a control character (a newline would add a second line to curl's config). Random passwords are hex and were never affected.

#43 also registers bwopenchamber's database lock with the holder from fix(bwrap): keep the database lock held under pasta without a log tool (the last commit here), so bwopenchamber is covered the same way as bwopencode2.

Nothing to change on this PR for these. If you would rather have them here than wait for #43, say so and I will move them.

Assisted-by: opencode:claude-sonnet-5-5
Assisted-by: opencode:gpt-6-astra

Stuart B. Wilkins (stuwilkins) added a commit to stuwilkins/n2snscripts that referenced this pull request Nov 16, 2026
…caping, hold bwopenchamber's lock

Copilot's review of NSLS2#37 named two problems in the --serve code, and a third gap is closed here.

- Port claims: two launches that start together and both see a stale claim file could each remove the other's new file and end up with the same port. serve_choose_port now holds an flock on claims.lock in the state directory while it cleans up stale claims and claims a port, and releases it before the sandbox starts (without flock it warns, as the publish-address claim does). tests/test_serve_claims.py starts six launches at once against stale files, and a launch that must wait for a held lock; both fail without the lock.
- Password: both pairing calls already escape a backslash and a double quote before writing curl's config. Add the missing test for the host-side one (serve_request_code), and refuse a BWOPENCODE2_SERVE_PASSWORD with a control character, a newline for one, which would add a second line to curl's config. The help texts and docs say so.
- bwopenchamber registers its database lock with net_hold_fd, as bwopencode2 does since the holder commit in NSLS2#37, so its lock is held under pasta without a log tool. Tests start the real guard against a temporary data directory for both launchers.
- docs/bw-wrappers.md: the lock under a private network is now held by the holder (it described the accident), and the claims lock is described.

Assisted-by: opencode:claude-sonnet-5-5
Assisted-by: opencode:gpt-6-astra
@stuwilkins

Copy link
Copy Markdown
Contributor

Pushed one commit to this branch: fix(bwrap): harden the database lock holder and the log condenser after review. It follows a full four-model review (GPT-6 Astra, GPT-6.1 Sol, Claude Fable 5.2, Claude Opus 5.5) of the three commits I added here (the user= field, the Copilot fixes, the lock holder), reviewed together. Nobody found a serious defect. The real findings, all fixed:

  • The holder could drop the lock if a fork failed. Its liveness check ran $(...), and a failed fork read as "the launcher has gone". The holder now runs no external command: it waits with read -t 1 on a private FIFO and reads its parent pid from /proc with builtins, and anything unclear means "still there". That also removes the sleep it would have looked up through PATH (or loaded with LD_PRELOAD).
  • Helpers forked before the holder kept a copy of the lock. On a host without Python, the bash log supervisor, blocked on a log tool that stopped reading, would have held the lock after the sandbox ended. The log reader or supervisor and the slirp4netns helper now close the registered descriptors first.
  • condense report on a dead stderr. The "log tool stopped reading" message is written last and guarded, so a closed or stalled stderr no longer leaves the session directory and the killed tool behind.
  • Smaller: the user-name filter runs in the C locale; comments and docs wrongly said the slirp4netns identity mapper closes the descriptor (it does not; the relay can reuse the number); the fuser hint and the journalctl example were wrong; a few weak tests were tightened.

Tests: the holder now has no child process over time and holds only the lock and one FIFO; a test fails if the helpers' close function is removed; a closed stderr no longer stops the condenser's cleanup. 80 Python tests and the 80 guard checks pass, the new ones also on a single core; the hooks pass. Still not tested on a real pasta launch or on RHEL 8's bash 4.4 and Python 3.6.

Left as it was: the holder watches the launcher, not the sandbox, and holds the lock next to OpenCode, not inside it (passing it down the descriptor relay is still the stronger design, and still your call); the bash fallback for hosts without Python still writes to the log tool synchronously and calls sleep through PATH; under --share-net the lock stays safe only because its descriptor number is above the relay's range.

Assisted-by: opencode:claude-sonnet-5-5
Assisted-by: opencode:gpt-6-astra
Assisted-by: opencode:gpt-6.1-sol
Assisted-by: opencode:claude-fable-5-2
Assisted-by: opencode:claude-opus-5-5

Copilot AI balanced review requested due to automatic review settings November 16, 2026 21:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed to evaluate a critical port-claim race alongside credential-escaping and failure-cleanup issues omitted from the published review.

No open issues 🎉

Review effort: Balanced

Stuart B. Wilkins (stuwilkins) added a commit to stuwilkins/n2snscripts that referenced this pull request Nov 16, 2026
…caping, hold bwopenchamber's lock

Copilot's review of NSLS2#37 named two problems in the --serve code, and a third gap is closed here.

- Port claims: two launches that start together and both see a stale claim file could each remove the other's new file and end up with the same port. serve_choose_port now holds an flock on claims.lock in the state directory while it cleans up stale claims and claims a port, and releases it before the sandbox starts (without flock it warns, as the publish-address claim does). tests/test_serve_claims.py starts six launches at once against stale files, and a launch that must wait for a held lock; both fail without the lock.
- Password: both pairing calls already escape a backslash and a double quote before writing curl's config. Add the missing test for the host-side one (serve_request_code), and refuse a BWOPENCODE2_SERVE_PASSWORD with a control character, a newline for one, which would add a second line to curl's config. The help texts and docs say so.
- bwopenchamber registers its database lock with net_hold_fd, as bwopencode2 does since the holder commit in NSLS2#37, so its lock is held under pasta without a log tool. Tests start the real guard against a temporary data directory for both launchers.
- docs/bw-wrappers.md: the lock under a private network is now held by the holder (it described the accident), and the claims lock is described.

Assisted-by: opencode:claude-sonnet-5-5
Assisted-by: opencode:gpt-6-astra
via web ui

Assisted-by: copilot:unknown
Only the start and end records carried user=, so a dns, open, close or
fail line could be attributed only by looking up its session. Every
record now begins session=ID user=NAME, written by the condenser
(--user) and by the fallback supervisor used without Python.

The name is the account of the launcher process (id -un), not $USER,
which the caller can set, and characters that could split a logfmt
field are replaced. The start and end records use the same value.

Assisted-by: opencode:claude-sonnet-5-5
- Keep draining pasta's log when the log tool stays alive but stops
  reading: records wait in a queue bounded in bytes (8 MiB) for a writer
  thread that writes what is waiting in one go, are dropped and counted
  when it is full (the next record carries a note), and the tool is
  killed at the end if it has not taken them. The end record and the
  queue share one 5 s deadline. Without a log command the records still
  go straight to standard output.
- Forward the piped-stderr bridge's bytes as they arrive and unchanged,
  instead of decoding and holding them until a newline.
- Record via= when only the port differs (--host-port 5432:15432).
- Log a complete TCP DNS query that fails upstream (rcode=TIMEOUT), once;
  a client that closes between or inside queries, or sends less than a
  DNS header, is not logged.
- tests/fake-net-run: bind a custom FAKE_NET_DIR at the fakes' fixed path,
  so settings survive the launchers' environment scrub.
- Say in the docs and the launcher comment that the bash fallback (no
  Python) still writes to the tool synchronously.

Assisted-by: opencode:claude-sonnet-5-5
Assisted-by: opencode:gpt-6-astra
Assisted-by: opencode:gpt-6.1-sol
Assisted-by: opencode:claude-fable-5-2
Assisted-by: opencode:claude-opus-5-5
The database guard's session lock is a descriptor of the launcher, and a flock lasts only while some process holds it open. With a private network the launcher ends in an exec into pasta, which closes every inherited descriptor above 2, so the lock survived only because the network log helper, forked just before the exec, happened to hold a copy. With no working log tool, or if the helper died, the lock was released while OpenCode ran, and another host or build could pass the guard. (With slirp4netns the descriptor can survive the exec, but the descriptor relay may reopen its number for a data file.)

net_hold_fd registers descriptors, and _net_hold_start starts a background holder before the exec that keeps them open until the launcher process has gone. It runs on the host, ignores the session's signals, closes every other inherited descriptor (slirp4netns exits only when bwrap closes its sync descriptor), waits with /usr/bin/sleep and not one found through PATH (which can include a directory the sandbox can write to), and does not depend on logging. bwopencode2 registers the guard's descriptor. With --share-net nothing changes: the descriptor still passes through exec.

The lock is still held next to OpenCode, not by it, and the holder watches the launcher, not the sandbox; passing the lock down the descriptor relay is the stronger design and is not done here. docs/bw-wrappers.md and docs/opencode2-db.md say so. Tests: tests/test_net_hold.py, with a stand-in that closes descriptors as pasta does, checked from outside with flock; each check was seen to fail with the holder's descriptor closing or signal handling removed.

Assisted-by: opencode:claude-sonnet-5-5
Assisted-by: opencode:gpt-6-astra
…er review

A four-model review of the lock holder, the log condenser and the user= field found no serious defect and these real ones, all fixed here.

- The holder waits with the shell's own read -t on a private FIFO (made, opened and unlinked before the fork) and reads its parent pid from /proc with builtins, so it runs no external command: nothing it could find through PATH or be made to load with LD_PRELOAD, and no fork that can fail while the sandbox uses up the process limit. The old liveness check forked a subshell, and a failed fork read as 'the launcher has gone', releasing the lock. Now anything but a parent pid that differs from the launcher's means 'still there'. Without a FIFO it falls back to /usr/bin/sleep with the LD_* variables unset.
- The helpers forked before the holder (the log reader or the bash supervisor with its log tool, and the slirp4netns helper) kept a copy of the lock descriptor; the supervisor, blocked on a log tool that stopped reading, would have held the lock after the sandbox ended. They now close the registered descriptors first.
- condense: the 'log tool stopped reading' report is written last and guarded, so a closed or stalled stderr no longer leaves the session directory and the killed tool behind or ends the process with status 120; its counts are worded as lower bounds.
- _net_log_user filters in the C locale; comments and docs said the slirp4netns identity mapper closes the descriptor (it does not; the relay can reuse the number); the fuser hint pointed at the wrong directory; the journalctl example matched longer user names; the supervisor comment and AI_DISCLOSURE.md prose were out of date.
- Tests: the holder test now checks that the holder has no child process over time and only the lock and one FIFO open, and that the helpers' close function matters (each fails without its change); a closed stderr no longer stops the condenser's cleanup; weak assertions removed or tightened; one rare reset in a TCP DNS test is accepted as the close it means.

Assisted-by: opencode:claude-sonnet-5-5
Assisted-by: opencode:gpt-6-astra
Assisted-by: opencode:gpt-6.1-sol
Assisted-by: opencode:claude-fable-5-2
Assisted-by: opencode:claude-opus-5-5
@maatthias
Matt Snyder (maatthias) merged commit 39e8154 into NSLS2:main Nov 19, 2026
1 check passed
Thomas A Caswell (tacaswell) pushed a commit to stuwilkins/n2snscripts that referenced this pull request Nov 19, 2026
…caping, hold bwopenchamber's lock

Copilot's review of NSLS2#37 named two problems in the --serve code, and a third gap is closed here.

- Port claims: two launches that start together and both see a stale claim file could each remove the other's new file and end up with the same port. serve_choose_port now holds an flock on claims.lock in the state directory while it cleans up stale claims and claims a port, and releases it before the sandbox starts (without flock it warns, as the publish-address claim does). tests/test_serve_claims.py starts six launches at once against stale files, and a launch that must wait for a held lock; both fail without the lock.
- Password: both pairing calls already escape a backslash and a double quote before writing curl's config. Add the missing test for the host-side one (serve_request_code), and refuse a BWOPENCODE2_SERVE_PASSWORD with a control character, a newline for one, which would add a second line to curl's config. The help texts and docs say so.
- bwopenchamber registers its database lock with net_hold_fd, as bwopencode2 does since the holder commit in NSLS2#37, so its lock is held under pasta without a log tool. Tests start the real guard against a temporary data directory for both launchers.
- docs/bw-wrappers.md: the lock under a private network is now held by the holder (it described the accident), and the claims lock is described.

Assisted-by: opencode:claude-sonnet-5-5
Assisted-by: opencode:gpt-6-astra
@tacaswell
Thomas A Caswell (tacaswell) deleted the feat/bwrap-private-netns branch November 20, 2026 03:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants