Server never exits on SIGTERM/SIGINT after serving a native file route while the JSON access log is enabled (stderr lock held by access-log thread)

Open Beginner friendly
#277 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
83/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust

Research direction

The report points to src/telemetry.rs around write_access_log and src/http_server.rs around the transfer high-water mark diagnostic. Read the access-log writer loop first, then reproduce with the provided Roc app and SIGTERM steps. Done when host diagnostics no longer deadlock with JSON access logging and the server exits after the static-file request.

Written by the indexing model from the issue text.

Description

With a JSON-lines access log enabled, a server that has served any native file route never exits on SIGTERM or SIGINT. shutdown! never runs, the 30 s drain deadline never forces an exit, and a second Ctrl-C does nothing. Only kill -9 stops it.

Reproduction

basic-webserver 0.22.0, Roc 0.1.0 (Homebrew), macOS 26.2 arm64.

app [Context, program] {
	http: "https://github.com/roc-lang/http/releases/download/1.0.0/6ZUwqYhCS8PU9Mo6MF7oV82ET2o7KYb57CLKDq4cq4sS.tar.zst",
	pf: platform "https://github.com/roc-lang/basic-webserver/releases/download/0.22.0/BM2BUXA8mwbcoKPUfRV8cLtsUdnsSBPwjNLPRUJQAQ7Q.tar.zst",
	roc: "0.1.0",
}

import pf.Path
import pf.Server
import pf.Stdout
import http.Response

Context : {}

program = { init!, respond!, shutdown! }

init! : () => Try({ config : Server.Config, context : Context }, [Exit(I64)])
init! = || {
	files = Server.file_root({ id: "public", path: Path.utf8("public") })
	config =
		Server.default_config
			.with_file_roots([files])
			.with_native_routes({
				files: [Server.static_mount({ at: "/static", files })],
				liveness: [],
				readiness: [],
			})
			.with_access_log(Server.json_lines_access_log({ target: Server.path_without_query, max_buffered_events: 256 }))
	Ok({ config, context: {} })
}

respond! : Server.Request, Context => Try(Server.Outcome, [ServerErr(Str)])
respond! = |_request, _context| Ok(Server.respond(Response.from_status(200).with_body(Str.to_utf8("roc"))))

shutdown! : Server.ShutdownReason, Context => Try({}, [Exit(I64)])
shutdown! = |reason, _context| {
	Stdout.line!("shutdown! ran: ${Str.inspect(reason)}") ?? {}
	Ok({})
}
mkdir public && echo hi > public/a.txt
roc build main.roc
./main & P=$!; sleep 1.5
curl -s localhost:8000/static/a.txt
kill -TERM $P   # the process is still running 45 s later; shutdown! never prints

Results, 10 runs each unless noted (exit checked within 2 s):

Access log Requests before SIGTERM Result
on static file hangs 10/10; still running after 45 s
on static 404 (/static/missing.txt) hangs
on Roc route only, or none exits in about 1 s
off static file exits 10/10

file_root_with_cache and open_metrics make no difference.

Cause

A sample of the hung process shows the main thread inside http_server::run_tcp_server, in std::io::stdio::_eprint, blocked on the stderr reentrant mutex (__psynch_mutexwait). The basic-webserver-access-log thread is idle in mpmc::Receiver<String>::recv, and every tokio worker is parked.

write_access_log takes the stderr lock once and keeps it for the thread's lifetime, including while it blocks waiting for the next line:

https://github.com/roc-lang/basic-webserver/blob/4536fe5/src/telemetry.rs#L851-L853

let stderr = io::stderr();
let mut writer = BufWriter::new(stderr.lock());
while let Ok(line) = receiver.recv() {

Every eprintln! on another thread therefore blocks until the access-log thread exits, and it does not exit before shutdown finishes. After a native file transfer, run_server_with_listener prints the transfer high-water mark after draining, so shutdown deadlocks there:

https://github.com/roc-lang/basic-webserver/blob/4536fe5/src/http_server.rs#L3051-L3055

Without a file transfer the high-water mark is 0, nothing is printed, and shutdown completes. The same lock explains why the safety valves don't help: the drain-deadline exit (Graceful drain exceeded ...) and the Second termination signal received; forcing process exit path both call eprintln! before process::exit. The request-body sink high-water message and every other host eprintln! (Invalid Roc response, connection errors, and so on) can block the same way while the access log is active.

Possible fix

Lock stderr per line instead of for the thread's lifetime, for example let mut writer = io::stderr().lock(); inside the loop around each writeln! and flush, or BufWriter::new(io::stderr()) so each write locks briefly. Then host diagnostics interleave with access-log lines at line boundaries instead of blocking.

Dominant language
HTML
Stars
110
Forks
25
Avg merge
18h 28m
Merged PRs (30d)
16

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from roc-lang/basic-webserver

All issues in roc-lang/basic-webserver

Similar issues

More Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.