8000 fix(prometheus): stop exporter threads on drop by maxi-maxima · Pull Request #1061 · wingfoil-io/wingfoil · GitHub
[go: up one dir, main page]

Skip to content

fix(prometheus): stop exporter threads on drop - #1061

Merged
0-jake-0 merged 2 commits into
wingfoil-io:mainfrom
maxi-maxima:agent/prometheus-shutdown-20261021
Oct 25, 2026
Merged

0-jake-0 merged 2 commits into
wingfoil-io:mainfrom
maxi-maxima:agent/prometheus-shutdown-20261021

Conversation

@maxi-maxima
@maxi-maxima maxi-maxima commented Oct 21, 2026 •
Copy link
Copy Markdown
Contributor

What this changes

Dropping a PrometheusExporter now closes every listener it started and joins its HTTP threads, including when a client leaves a request unfinished or stops reading a response. Keep the exporter alive for as long as the endpoint is needed; the Rust and Python API docs now say this explicitly.

Why

Refs #455, item 8 only. The previous serve() discarded its JoinHandle while the detached thread retained the listener and registry. A regression test against main reproduces the leak: binding the same port after dropping the exporter fails with AddrInUse.

The exporter now owns the shutdown channels and thread handles. Disconnecting the channel stays observable before every accept/read/write, including a connection accepted during teardown. Socket operations retry short timeouts below BufReader, preserving partial UTF-8 and the five-second inactivity budget while making shutdown observable on Windows too. The sink's ArcSwap path, synchronous bind errors, serve(&self) signature, and support for repeated port-0 serve calls are preserved. No dependencies are added.

How it was verified

  • cargo fmt --all -- --check, cargo lint, cargo lint-all, and the setup-dev, clippy-SARIF, example-docs, and Python-binding guard scripts: pass.
  • cargo test --locked -p wingfoil --all-features --lib --tests --no-run, then all 78 test binaries that do not require external services: 1,407 tests pass. The 15 live-service integration binaries were compiled but not executed.
  • cargo test --locked -p wingfoil-derive: 20 tests pass. cargo test --locked --doc -p wingfoil --all-features: 66 pass, 29 ignored.
  • cargo test --locked -p wingfoil-python --features all-adapters: 284 tests pass. maturin develop --locked -F all-adapters and pytest -q: build passes; 435 tests and 15 subtests pass, 4 skipped, 47 deselected. The focused Prometheus suite passes all 11 tests.
  • Native Windows/MSVC: all 7 Prometheus adapter tests and 6 lifecycle unit tests pass, using the same source as the Linux checks. These cover port release, repeated servers, partial requests, blocked readers/writers, accept/drop races, split UTF-8, and the inactivity timeout.
  • The port-release regression fails against the original implementation. Final source hashes match the tested copies. No benchmarks or socket/thread-creation fault injection were performed.

Notes for the reviewer

Updated after #1062 merged: merged current main (b15142e) forward in 0df22cc, without rewriting history. The six-file Prometheus diff is unchanged; dependency files match current main. Fresh Linux/Rust 1.99 10000 validation passes fmt, both Clippy feature sets, 1,412 non-service core tests, 20 derive tests, 68 doctests (30 ignored), and 284 Python Rust tests. All eight executed upstream checks now pass, including both dependency audits, Python Interop and the live Prometheus integration job; coverage is intentionally skipped and the Clippy report is neutral. The native Windows and full Python pytest results above are from the original implementation validation and were not rerun for this upstream merge.

Accept polls at 50 ms while idle; disconnecting the shutdown channel wakes it immediately. Dropping the owner interrupts active scrapes rather than waiting for them to finish. This deliberately replaces the detached-server lifetime, so a temporary exporter no longer leaves a server running.

Before you submit

  • Allow edits by maintainers is ticked

@maxi-maxima
maxi-maxima marked this pull request as draft October 21, 2026 02:39
@maxi-maxima
maxi-maxima marked this pull request as ready for review October 25, 2026 02:13
@0-jake-0 0-jake-0 left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ran the new unit tests, the prometheus_adapter integration file, fmt and clippy locally on this head: all green. The design holds up: the exporter owns the Servers, dropping the Sender is observable at every accept/read/write, the short socket timeouts retried below BufReader keep split UTF-8 intact (and the test proves it), and the type stays Send + Sync. The drop_can_race_with_accept loop and the "scrape that is not reading" case are the two races I would have asked for. Recording it as deviation 3 in both mod.rs and the adapter CLAUDE.md, plus the Python docstring and README, is the right paperwork for a lifetime change users can hit silently (wf.PrometheusExporter(..).serve() as a one-liner now stops immediately).

Issue #455 item 8 said "adopt the WebServer pattern", which is tokio graceful shutdown; for a std::net listener this shutdown-channel-plus-join is the honest equivalent. One alternative worth knowing about if the 50 ms idle wake-up ever matters: keep accept blocking and wake it on drop by connecting to the bound address. Not asking for it here.

Two non-blocking inline nits. Merges cleanly onto current main.


Generated by Claude Code

}
}

let mut conn = reader.into_inner();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Non-blocking: when the shutdown lands mid-request (the b"GET /met" case in drop_interrupts_idle_and_partial_requests), read_line returns the partial line as Ok(n), so the handler falls through to the 404 branch, write returns Ok(0), write_all fails with WriteZero, and we warn! "failed to write 404 response" on every drop with a connection in flight. A shutdown.try_recv() check here (return quietly if disconnected) would keep drop silent. Cosmetic, happy for it to ride a later push.


Generated by Claude Code

assert_eq!(line, "é\n");
thread.join().unwrap();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Non-blocking: this one costs a real five seconds of wall clock in the --lib suite (the module's six tests take 5.05 s locally, this is all of it), and the pre-push hook runs that suite. If you want to keep the assertion, threading the budget through Connection (or a const fn with a test override) would let it assert "longer than one poll interval, shorter than forever" in ~100 ms instead.


Generated by Claude Code

@0-jake-0
0-jake-0 merged commit 4a2c6db into wingfoil-io:main Oct 25, 2026
10 checks passed
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

0