Skip to content

fix: reuse the read connection for vss writes - #147

Merged
ovitrif merged 1 commit into
mainfrom
fix/vss-write-shared-pool
Dec 1, 2026
Merged

ovitrif merged 1 commit into
mainfrom
fix/vss-write-shared-pool

Conversation

@ovitrif

@ovitrif ovitrif commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Refs: synonymdev/bitkit-android#2219

Description

  • Fixes slow node starts since 0.7.0-rc.82. Since then VSS writes have had their own VssClients, each with a new reqwest client, so the first channel monitor write while the node builds opened a new connection instead of reusing the one the reads had just opened. On a phone that adds a DNS lookup, TCP connect and TLS handshake to every start, plus the retries of a connect that fails.
  • Each side's read and write clients now share one reqwest client (blocking_client with blocking_write_client, async_client with async_write_client), built with the timeouts VssClient::new_with_headers uses. Writes still retry through fenced_retry, and the blocking and async sides keep separate connections.

Timings (staging VSS through a local proxy, 150 KB write to monitors/, 2 rounds each):

2 s added to each new connection First 4 new connections fail
rc.80 first sync monitor write 0.21–0.37 s no failed attempts
rc.84 2.33–2.41 s 5 attempts, 0.63–0.92 s
this PR 0.38 s no failed attempts

On a good network with no proxy, rc.84 writes on the first attempt in 0.2–0.45 s on staging, api.bitkit.to and bitkit.to. A full-node restart on regtest with staging VSS shows the monitor write at 0.12 s. Phone start times on this build haven't been measured yet.

Out of Scope

  • Release: the next release candidate and the twin app bumps that ship this
  • Logging of failed write attempts in fenced_retry: they stay silent unless the write fails

QA Notes

Manual Tests

  • Android or iOS mainnet start with an open or recently closed channel → Got new ChannelMonitor → Persistence … completed back near 0.3–0.4 s, as on 3.1.1 — needs a phone network

Automated Checks

  • added connection_tests::first_write_reuses_the_read_connection in vss_store.rs: a local VSS stub counts connections; it fails on main with 2 connections where 1 is expected
  • ran cargo test --lib vss_store

Models used

  • Planning/scoping: Claude Opus 5.5 (reasoning: medium)
  • Implementation: Claude Opus 5.5 (reasoning: medium)
  • Review: Gemini 4 Neon (reasoning: high)
  • Review rounds: 1

Since 0.7.0-rc.82 writes went through their own VssClients, each with a
new reqwest client, so the first write after a node build opened a new
connection instead of reusing the one the reads had just opened. On a
phone that put a DNS lookup, TCP connect and TLS handshake, and any
retries of a failing connect, into the first channel monitor write while
the node builds.

Each side's read and write clients now share one reqwest client, built
with the timeouts VssClient::new_with_headers uses. Write retries stay
in fenced_retry.

Verified: cargo test --lib vss_store (the new connection test fails on
main with 2 connections); staging VSS through a local proxy adding 2 s
per new connection: the first sync monitor write takes 0.38 s, against
2.33-2.41 s on rc.84 and 0.21-0.37 s on rc.80
@ovitrif ovitrif self-assigned this Dec 1, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-01T13:02:56.899792Z e36d347 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@talosmachina talosmachina 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.

No findings. Each side's VSS read and write clients now share one reqwest client, so the first monitor write reuses the connection the reads opened instead of paying DNS, TCP and TLS again. Reviewed e36d347, full tier, static.

What I checked, and 5 candidates I ruled out

Read in full: the diff, VssStore::new, fenced_retry, the KVStore / KVStoreSync read and write paths in src/io/vss_store.rs, and vss-client-ng 0.4.1 src/client.rs as locked.
Not built or run here: my sandboxed runner refused to build this PR, so this review is from the code. CI ran only mobile-builds-unwind (green). rust.yml and vss-integration.yml are workflow_dispatch only, so connection_tests::first_write_reuses_the_read_connection has not run in CI.

Ruled out

  • Timeout drift: build_http_client matches build_client() in vss-client-ng 0.4.1 (10 s total, connect and read). Writes keep the per-attempt bound from fenced_retry, as before.
  • Reads queued behind a slow write: reqwest 0.12 here is HTTP/1 only (no h2 in the lockfile's reqwest 0.12.28 entry). The pool hands out only idle connections, so a hung write makes a read open a new connection rather than wait for it.
  • Fencing: sharing a client changes only the transport. begin_write, the generation check before each attempt and the no-retry policy on the write clients are untouched.
  • Cross-runtime pooling: both sides' requests already run on internal_runtime (block_on on its handle, or runtime.spawn), so a pooled connection is never driven from a runtime that did not open it.
  • Test determinism: the stub counts each accept before it serves that connection, so a second connection would show up in the count before the write returns. One narrow window remains: hyper-util 0.1.20 can hand the read's connection back to the pool from a spawned idle task (client.rs:358-362). If the write checks out a connection before that task runs, it opens a second one. Only the async read then write pair in the test has little work between the two, so if this test ever flakes on 2 != 3, that is where to look. It does not affect the fix.

Merge confidence: 4/5, no findings, but the test covering the change has not run in CI and I could not run it here.

@piotr-iohk piotr-iohk 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.

QA review

Scope: full PR diff against its merge base, at e36d347.

No new actionable code findings.

No new actionable code findings. Sharing one reqwest::Client between blocking_client and blocking_write_client (and a separate shared client between async_client and async_write_client) via VssClient::from_client_and_headers preserves vss-client-ng 0.4.1's 10 s total/connect/read timeouts while returning io::Error instead of panicking on client build failure, and keeps StoreFence, fenced_retry / no_retry_policy, and sync-vs-async pool isolation intact. External reviews on PR #147 (talosmachina, Codex) also reported no open findings.

Validation:

  • Executed locally at e36d347 (Rust 1.100.1): cargo test --lib vss_store --locked with RUSTFLAGS="-D warnings" passed (25/25, including connection_tests::first_write_reuses_the_read_connection, which also passed 100/100 consecutive runs); cargo test --lib --locked passed (164/164); cargo check --features uniffi --locked, cargo doc --no-deps --locked, and rustfmt --edition 2021 --check src/io/vss_store.rs passed; cargo clippy --lib --locked reported no warnings on changed lines.
  • CI: mobile-builds-unwind passed; build (ubuntu-latest, stable) failed on pre-existing cargo fmt --all -- --check diffs in untouched files (tests/common/mod.rs, src/builder.rs, src/lib.rs, src/payment/asynchronous/static_invoice_store.rs, src/payment/onchain.rs, tests/integration_tests_rust.rs, tests/multi_address_types_tests.rs), cancelling the remaining jobs in that matrix run before tests ran.
  • Remaining checks: the vss_test integration suite against a live VSS server was not run here (no VSS/Postgres service in this environment), and the mobile-network startup latency check referenced by synonymdev/bitkit-android#2219 remains to be verified on Android/iOS once shipped in a downstream release candidate.

Reviewer model: gemini-4-neon
Reasoning effort: high

@coreyphillips coreyphillips left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Two independent reviews, nothing blocking a merge.

@ovitrif

ovitrif commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

I dispatched the two on-demand workflows on this branch so the new test would run in CI. Neither gets that far, for reasons that are also on main:

  • CI Checks - Rust Tests fails at the format check. Current stable rustfmt wants changes in src/builder.rs, src/lib.rs, src/payment/onchain.rs, src/payment/asynchronous/static_invoice_store.rs and tests/, none of which this PR touches. src/io/vss_store.rs passes.
  • VSS Integration fails at cd vss-server/rust: the checked-out vss-server no longer has that path.

Locally, cargo test --lib vss_store passes 60 times out of 60 in a row, including connection_tests::first_write_reuses_the_read_connection, with the other tests running in parallel.

@ovitrif

ovitrif commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

Merged: approved by @coreyphillips and @piotr-iohk on e36d347, mobile-builds-unwind green. Next: release 0.7.0-rc.85 for the app bumps.

@ovitrif
ovitrif merged commit a096c13 into main Dec 1, 2026
1 of 11 checks passed
@ovitrif
ovitrif deleted the fix/vss-write-shared-pool branch December 1, 2026 13:21
ovitrif added a commit to synonymdev/bitkit-android that referenced this pull request Dec 1, 2026
Bumps ldk-node to 0.7.0-rc.85: VSS writes reuse the connection the store's reads opened (synonymdev/ldk-node#147), so the first ChannelMonitor write at node start doesn't wait on a new connect and its retries (#2219).

Verified: ./gradlew --write-verification-metadata sha256 help resolves ldk-node-android 0.7.0-rc.85 from GitHub Packages and records its checksums
ovitrif added a commit to synonymdev/bitkit-android that referenced this pull request Dec 1, 2026
Bumps ldk-node to 0.7.0-rc.85 on master, matching release-3.2.0: VSS writes reuse the connection the store's reads opened (synonymdev/ldk-node#147), so the first ChannelMonitor write at node start doesn't wait on a new connect and its retries (#2219).

Verified: none (same pins and checksums as the release-3.2.0 bump, which resolved rc.85 from GitHub Packages and a clean SwiftPM cache)
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