Skip to content

Fix two bugs: threads not sharing chdir, and a --dir of a single file - #363

Merged
progrium merged 5 commits into
mainfrom
claude/project-thread-kswdn5
Nov 28, 2026
Merged

progrium merged 5 commits into
mainfrom
claude/project-thread-kswdn5

Conversation

@progrium

@progrium progrium commented Nov 28, 2026 •

Copy link
Copy Markdown
Owner

Requested by Jeff · project thread

Two bug fixes found running real programs through h9x. Bug fixes, so no proposal. Each is its own commit.

Threads share the working directory

Found running elixir-plug/plug through h9x, once #361 let Mix fetch Hex.

On Linux, a process's threads share their working directory (CLONE_FS). In h9, each thread is its own kernel process with its own dot, so a chdir in one thread left the others where they were. Erlang's file server changes directory in one thread, and Mix compiles relative paths in another, so plug's deps step failed with could not read file "lib/mime/application.ex".

The thread group now holds a clone of the directory a member last changed to, with a generation number. A member whose generation is behind takes that directory at the top of its next call (syncDot in dispatch). A process with only one thread shares nothing, and a new thread starts in its creator's directory. The kernel side gains h9_linux_dotget, h9_linux_dotset, and h9_linux_chanref.

New [linux-threads] case cwd_threads: a thread's chdir moves the main thread, the main thread's chdir moves a later thread, and a thread's fchdir moves the main thread. spec/abi-linux.md's thread paragraph now says the group holds the working directory.

The go-stdtest thread had the same fix for Go's TestProgWideChdir and is dropping its copy in favor of this one.

A --dir of one file

A --dir whose host path is a file, such as --dir app.conf:#L/etc/app.conf:ro, became a directory attach on the stack member and failed with not a directory.

Now resolveMount marks a --dir whose host path is a file, and the machine binds such a mount over the file's path on the stack after the stack is built, making the path and its parents when they're missing. A target that is a directory is refused. A file mount doesn't make the stack's member writable, so rw writes reach the host file while every other write still goes to the overlay. spec/cli.md's --dir row describes this.

New [mounts] test: a read-only file at a new, deep path, and a read-write file whose append reaches the host, with a write to the read-only one refused.

The branch merges its own already-merged #361 commits so that the push needed no force. Only the files above differ from main.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LLcnT1EGgD3xQKfCsAjjvA

…TCONN

A datagram conversation has no host socket until it binds to a port,
connects, or sends. Until then ipready answered with the default (ready
to read and write) and recvfrom with ENOTCONN, so a poll on a fresh UDP
socket said it was readable and the read failed. Erlang's resolver
polls its new UDP socket and logged the ENOTCONN as an error
({udp_error, Port, enotconn}) during Mix's Hex fetches. Now it's
writable only and a nonblocking recv is EAGAIN, as on Linux, and
creating the host socket kicks the watchers, so a recv blocked before
the socket existed wakes for the reply to its first sendto.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LLcnT1EGgD3xQKfCsAjjvA
… a rearm isn't missed

An epoll_ctl in another thread changes the interest list and then moves
the readiness generation (pollWake). epoll_wait copied the list and only
then took the generation, from its first poll, so an EPOLL_CTL_MOD that
rearmed an EPOLLONESHOT entry between the two was counted in the
generation the wait slept on while the copy still had the entry
disarmed: the wait slept with the fd ready until something else woke it.
Erlang's poll thread waits that way while its schedulers rearm each
socket after a read, so a TLS download stopped partway and Mix's Hex
fetches timed out after 60 seconds, in about one run in three. The
generation is now taken before the copy.

The new epoll_rearm case reads loopback connections a segment at a time,
rearming after each, and failed 3 of 3 runs before (a connection
readable with no event) and passes 3 of 3 after.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LLcnT1EGgD3xQKfCsAjjvA
Linux's threads share the working directory (CLONE_FS), and each of
h9's threads is its own kernel process with its own dot, so a chdir in
one thread left the others where they were. Erlang's file server
changes directory in one thread and Mix compiles relative paths in
another, so building elixir-plug/plug failed: could not read file
"lib/mime/application.ex". The group now holds a clone of the
directory a member last changed to, with a generation, and a member
whose generation is behind takes it at the top of its next call. A
process with one thread shares nothing; a thread starts in its
creator's directory.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LLcnT1EGgD3xQKfCsAjjvA

Copy link
Copy Markdown
Owner Author

Test results on 082f076 (the merge commit above it changes nothing), in a cloud container on the sud engine:

  • zig build test passes.
  • zig build integration -Darch=amd64 -Dengines=sud filtered to [linux and [mounts]: 169 pass, 64 skip, 0 fail.
  • cwd_threads fails without the sys.zig, kc.zig, and kern/linux.zig changes ("getcwd ... strcmp(b, sub) == 0") and passes with them.

Still unchecked: KVM, HVF, and WHP, the 9front cases, and plug's full mix test through h9x with this build.


Generated by Claude Code

A --dir whose host path is a file landed on a stack member as a
directory attach and failed with "not a directory". Now the bind goes
over the file's path on the stack, making the path (and its parents)
when it's missing, and rw writes reach the host file while the rest of
the stack keeps its overlay.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LLcnT1EGgD3xQKfCsAjjvA
@progrium progrium changed the title linux: a chdir or fchdir in one thread moves the process's other threads Fix two bugs: threads not sharing chdir, and a --dir of a single file Nov 28, 2026

Copy link
Copy Markdown
Owner Author

Test results at 2a3dc37 (adds the single-file --dir fix), on Linux amd64 with the sud engine:

  • zig build test: passes.
  • zig build integration -Darch=amd64 -Dengines=sud -Dfilter="[linux" -Dfilter="[mounts]": 170 pass, 64 skip, 0 fail (the earlier 169 plus the new [mounts] test).
  • The new test fails without the fix, with #Olinux/new/dir/app.conf: not a directory.
  • By hand: files at #L/one.txt, #L/etc/hostname (over an existing file), a deep new path, and /tmp/one.txt; ro writes refused; rw writes reach the host file while other writes go to the overlay.

Generated by Claude Code

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.

2 participants