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)

オープン 初心者向け
#277 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
83/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
rust

調査の方向性

報告では、src/telemetry.rs の write_access_log 付近と、src/http_server.rs の転送ハイウォーターマーク診断付近が指摘されています。まずアクセスログ書き込みループを読み、次に提供された Roc アプリと SIGTERM の手順で再現してください。ホスト診断が JSON アクセスログ記録と同時にデッドロックせず、静的ファイルへのリクエスト後にサーバーが終了すれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
HTML
スター
110
フォーク
25
平均マージ
18時間 28分
マージ済み PR(30日)
16

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

roc-lang/basic-webserver のほかの issue

roc-lang/basic-webserver の issue をすべて見る

似ている issue

Backend & API Design の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。