Repository navigation
Use libevent cached time for deadline-clamped reconnects - #812
Conversation
|
🤖 Automated first-pass review — a human maintainer's review is still required before merge. Thanks for the detailed write-up. The root cause makes sense to me: A few comments:
The core fix looks right to me. My only open question is whether the dependency bump is the trade-off we want, rather than anything about the code. |
|
The PR body now explicitly documents the 2.0.10 → 2.0.11 dependency change, its compatibility impact, and both old-header fixes. The automated review appears to have raced that body update. This floor is tested against the actual upstream 2.0.11 source, not just changed package metadata: a separate full memtier build with TLS and Prometheus disabled passes all 17 unit programs and the same five real reconnect/deadline RLTests (27 seconds). Restricted pkg-config discovery selects only the recorded 2.0.11 prefix. The independent BUILD reviewer is rechecking that artifact. Current general CI uses newer libraries; I am not claiming a dedicated 2.0.11 hosted CI job. Upstream 2.0.10 is explicitly rejected by configure. Its real clock probe returns monotonic uptime and a wrong 1.0-second delay for a wall deadline 0.5 seconds away; actual 2.0.11 produces the correct 0.5 seconds. The upstream fix is a459ef7 in the April 2011 release. The already-mutable address local and the unit's typed -1 value are precisely old-header compatibility fixes discovered by that build. No const is cast away, and the socket behavior is unchanged. The body documents both and calls out that users of 2.0.10 must upgrade; that dependency change should carry into the eventual release notes. The unit intentionally creates a default cached event base, and its callback-level comment describes the retained cache. Its new independent wall samples also prevent the old self-anchored test from accepting the wrong epoch. One clarification: absence of a cache normally makes Exact-head platform CI and the focused independent rechecks remain required before merge. |
|
bugbot run Please review integration head cf0591d. The automatic Cursor check skipped an unchanged patch; a fresh run is requested for this head. Following the 19:12:19 Claude update: a pinned minimum-libevent CI job would be useful follow-up coverage. This PR currently proves the floor with the retained actual 2.0.11 full build, 17 units and five runtime controls, rather than claiming that hosted CI already contains that cell. The integration preserves every production source, configure floor and original clock regression unit from e002d99. The two incoming #816 units received fresh 2.0.11 header checks; modern-library clean build passes all 17 units and five deadline regressions. Earlier minimum-library runtime results remain explicitly attributed to the earlier head. The mutable-address and typed-socket changes stay because the actual supported-minimum build needs them; neither changes runtime socket behavior. The compatibility impact remains documented. Integration proof and limits. Fresh Windows/macOS and all other CI checks, plus the seven focused reviews, remain required before merge. |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit cf0591d. Configure here.
Picks up #828 (TSan crash tests signal only while the main thread sleeps), which fixes this branch's red TSan crash-handler job, and #838. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks. Answers in order:
Since your update, I merged master into the branch (#828 fixes the TSan crash-handler job that was red here). The PR diff itself is unchanged. |
Picks up #812, the reconnect deadline-clock fix for the never-started client test that failed this branch's plaintext job, plus #828, #838 and #841. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ime version The master merge replaced #812's prerequisites sentence; restore its explanation next to the --disable-prometheus paragraph. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… failures (#823) ## Problem Part of the libevent support work (Refs #545). memtier did not check or record which libevent and OpenSSL it was built against and which it runs with, and a few libevent failures were not handled: - A binary built with one libevent's headers can load another series' library, for example headers from one prefix and the library from another, or a vendored header. Nothing reported this. The `--version` `libevent=` field shows the runtime library; `openssl=` shows the build headers. The JSON output had no library versions at all. - `evthread_use_pthreads()` / `evthread_use_windows_threads()` failing only printed a warning. Cross-thread `event_base_loopbreak()` (Ctrl+C, `--connection-stage-timeout`) depends on it, so the run could hang later. - `bufferevent_openssl_socket_new()` / `bufferevent_socket_new()` returning NULL in `shard_connection::setup_event()` hit `assert(m_bev != NULL)`, and the process aborted with SIGABRT. - `configure.ac`, DEVELOPMENT.md and README said the Prometheus exporter needs libevent 2.1.1. The exporter calls `evhttp_request_set_on_complete_cb()`, which first appeared in **2.1.4-alpha**: it is missing from `release-2.1.3-alpha:include/event2/http.h` and was added in upstream commit b083ca05. No document said which libevent versions are supported. - The comment above the READONLY `bufferevent_write()` said that call goes through `bufferevent_trigger_nolock_()`. In libevent 2.1.12 and 2.2.2-alpha, `bufferevent_write()` is `evbuffer_add(bufev->output, ...)`, the same path the protocol writers use (Refs #532). - `make check` did not compile on libevent 2.1.8 (RHEL 8): `tests/unit/cluster_transaction_test.cpp` used `EVUTIL_INVALID_SOCKET`, which first appeared in 2.1.9. #812 (now on master) fixed that hunk, so it is no longer in this diff; master builds and passes `make check` on Rocky Linux 8 (libevent 2.1.8, OpenSSL 1.1.1k). ## Fix - **Mismatch warnings (stderr only).** After option parsing, memtier compares `LIBEVENT_VERSION_NUMBER` with `event_get_version_number()`. If major.minor differ, it prints one warning to stderr. It does the same for OpenSSL, comparing `OPENSSL_VERSION_NUMBER` with `OpenSSL_version_num()` (`SSLeay()` on OpenSSL 1.0.2): before 3.0 it compares major.minor (1.0 vs 1.1); from 3.0 on it reports another major version or a library minor version older than the headers (3.5 built, 3.0 run), because OpenSSL 3 only promises that older headers run on a newer library. Patch levels and newer libraries are not reported, so the el9 RPM case (2.1.13 build, 2.1.12 runtime) stays quiet. Because the check runs after option parsing, `--version` and `--help` print nothing extra, and `--version` stdout is byte-identical. The comparison lives in the new `library_versions.h`. - **Provenance.** The crash report INFO section gains `libevent_build_version:` and `openssl_runtime_version:`. Existing keys are unchanged; note that the existing `openssl_version:` is the build-time text. `OpenSSL_version()` is pre-resolved like `event_get_version()`. The JSON `run information` section gains four new keys: `libevent runtime version`, `libevent build version`, `OpenSSL runtime version` and `OpenSSL build version`. The two OpenSSL keys appear only when TLS is compiled in. No existing key is renamed. - **evthread failure** is now fatal for a run. `evthread_use_pthreads()` still runs first, before any `event_base` exists; if it failed, memtier prints an `error:` line and calls `exit(1)` right after option parsing, like the other post-parse error exits, so LeakSanitizer reports nothing. `--help` and `--version` behave as before. This is a behaviour change for release notes: a run on such an install used to continue with a warning. - **bufferevent/SSL creation failure.** `setup_event()` now returns `bool`. On failure it logs an `error:` line and `connect()` returns -1, which is the existing path for a socket setup failure: standalone aborts the run in prepare, and cluster mode retries the connection and ends the run as failed. Ownership was checked in the libevent source: - With `BEV_OPT_CLOSE_ON_FREE`, a failed `bufferevent_openssl_socket_new()` already frees the `SSL` in libevent ≥ 2.1.9 (e86ccfe5, first released in 2.1.9-beta) and in 2.2.2-alpha. memtier therefore does **not** free it; doing so would be a double free. - The socket is not closed by libevent, because the SSL's socket BIO is `BIO_NOCLOSE`, and plain `bufferevent_socket_new()` does not close it either. memtier closes it. - On libevent ≤ 2.1.8 it depends on where libevent failed. An allocation failure leaves the socket open and leaks the SSL. A failure to arm the handshake events frees the partial bufferevent with the SSL attached, and its destructor closes both. memtier can't tell these apart, so it leaves the socket open, rather than risk closing a descriptor number that another thread has just been given, and logs `error: socket N left open on purpose: libevent <version> may already have closed it.` That is one socket (and possibly one SSL) per failed attempt, and only under libevent allocation failure. - The rule lives in `libevent_ssl_failure_socket_is_ours()` in `library_versions.h`, which is unit-tested. - `SSL_new()` returning NULL is handled the same way; it was an assert before. - `setup_event()` and `disconnect()` now clear the protocol's buffer pointers once the bufferevent is freed. Before, they kept pointing at the freed evbuffers until the next successful connect. - **Docs and configure.** The `libevent_extra` pkg-config check, the configure error message, DEVELOPMENT.md and README now say 2.1.4. The real gate, the link test, was already effectively 2.1.4, so no build that works today stops working. The core libevent minimum in configure is **unchanged**. DEVELOPMENT.md gains a "Supported libevent versions" table: - 2.1.8: oldest release known to work (RHEL 8), checked manually, not built in CI - 2.1.12: recommended minimum, runs the main CI suite - 2.1.13: current stable with security fixes - 2.2.2-alpha: experimental It also documents the mismatch warning, the new JSON keys, and the 2.1 vs 2.2 read size difference that makes results from the two series not directly comparable (#535). `tests/run_local_gate.sh` now checks for the 2.1.4 floor in DEVELOPMENT.md. - **#532 comment**: the comment is corrected. This is a comment-only change; the code path is untouched. ## Tests - New unit test `tests/unit/library_versions_test.cpp` (in `make check`) covers the series comparison. Cases: libevent patch levels and series; OpenSSL 3.x patch differences, a newer and an older minor, 3 vs 4, 1.0 vs 1.1 vs 3; LibreSSL. - New RLTest file `tests/test_library_provenance.py`. The LD_PRELOAD tests use the new `tests/library_failure_injector.c` and skip when not on Linux or when the binary is linked statically. Sanitizer runtimes are kept first in `LD_PRELOAD`, as in the crash-handler tests. - `test_json_run_information_library_versions`: the new JSON keys are present, `libevent runtime version` equals `--version`'s `libevent=` field, `OpenSSL build version` contains `openssl=`, the existing keys are still there, and a matched install prints no warning. - `test_libevent_series_mismatch_warns_on_stderr`: with a fake runtime libevent (2.99) and, in TLS builds, a fake OpenSSL, memtier prints exactly one warning per library on stderr and none on stdout. The JSON reports the runtime version. `--version` stderr stays empty. - `test_libevent_patch_difference_does_not_warn`: a 2.1.x patch difference prints no warning. - `test_evthread_failure_is_fatal`: exit 1, the `error:` line, no JSON written, and no LeakSanitizer report. `--help` and `--version` keep their exit code and stdout under the same failure. - `test_bufferevent_creation_failure_fails_connection`: a positive error exit (not a signal), the `error:` line naming the constructor, and no sanitizer report. TLS runs exercise `bufferevent_openssl_socket_new`, plaintext runs `bufferevent_socket_new`. The shim records the socket it was given and checks at exit that memtier closed it, using the descriptor's device/inode so that a reused number isn't mistaken for it. - `test_tls_bufferevent_failure_leaves_socket_on_libevent_2_1_8`: with a faked 2.1.8 runtime, the socket is left open and the log line appears. - `test_crash_report_separates_build_and_runtime_versions`: with faked runtime versions, the crash report's runtime fields show the fake and its build fields don't. The mismatch test checks the same for the JSON keys. - `test_crash_handler_with_active_connections` now also checks the new INFO lines, and that `libevent_version:`/`openssl_version:` equal `--version`'s `libevent=`/`openssl=`. - **Before/after**: on master d8d4e9f, 4 of the 5 new tests fail: - bufferevent: SIGABRT on `Assertion 'm_bev != NULL' failed`. - evthread: exit 0 with a warning. - JSON: the keys are missing. - mismatch: no warning. The patch-difference negative control passes on both. Two more tests were added later; reading the code says both also fail on master, but they haven't been run there. `test_tls_bufferevent_failure_leaves_socket_on_libevent_2_1_8` would hit the same `m_bev != NULL` assertion, and `test_crash_report_separates_build_and_runtime_versions` would find the new INFO fields missing. That makes 6 of the 7 tests in `test_library_provenance.py`, with the control passing. - Local results (Docker, aarch64; CI covers x86_64): | Variant | Build | make check | New tests + crash-handler tests | |---|---|---|---| | Ubuntu 24.04, system libevent 2.1.12, OpenSSL 3.0.13 | ✓ | 19/19 | standalone plaintext + TLS pass. Full TLS standalone suite: 727 pass / 109 skip / 1 fail (`test_prometheus:test_F16_ipv6`, no IPv6 in the container; unrelated) | | libevent 2.2.2-alpha (source) | ✓ | 19/19 | standalone plaintext + TLS, cluster TLS pass | | `--disable-tls` | ✓ | 19/19 | pass | | libevent 2.1.8 (source, OpenSSL 3.0.13) | ✓ | 19/19 | standalone plaintext + TLS, cluster TLS pass | | rockylinux:8, distro libevent 2.1.8-11.el8_10, OpenSSL 1.1.1k, GCC 8.5 | ✓ (TLS + Prometheus) | 19/19 (fails to compile without the test fix) | n/a | | ASan+UBSan (`detect_leaks=1`), system libevent | ✓ | | standalone plaintext + TLS, cluster TLS pass | - The libevent 2.1.8 tarball is the GitHub release asset, sha256 `965cc5a8bb46ce4199a47e9b2c9e1cae3b137e8356ffdad6d94d3b9069b71dc2`, which matches Debian's `libevent_2.1.8-stable-4.dsc`. Built from source on glibc ≥ 2.36, 2.1.8 does not link (`arc4random_addrandom` is undefined). That is a libevent 2.1.8 vs new glibc problem, not a memtier one, and RHEL 8 (glibc 2.28) is not affected; the local harness works around it with a no-op define. - clang-format 18 `--dry-run --Werror` is clean on all changed C++ files. ### When the mismatch check can fire Distribution packages tie the library file name to the series (`libevent-2.1.so.7`, `libevent-2.2.so.1`, `libssl.so.3`), so a binary built against 2.2 headers and run with `LD_LIBRARY_PATH` pointing at 2.1 fails to load before `main()`. The check therefore fires when the headers and the library come from different installations at build time: mixed `-I`/`-L` prefixes, a Homebrew plus source-built libevent, vendored headers, or a loader that resolves another series under the same name. The runtime path is covered by the preload shim. A real mix was also built locally: 2.2.2-alpha headers via `CPPFLAGS=-I<2.2.2 prefix>/include`, linked against the system 2.1.12 libraries. It builds and links, `--version` is unchanged (`libevent=2.1.12-stable`), and a run prints `warning: memtier_benchmark was built with libevent 2.2.2-alpha-dev headers but is running with libevent 2.1.12-stable; rebuild it against the libevent it runs with.` ## Affected versions - The assert on bufferevent creation failure and the evthread warning-only handling are in every current release. Hitting them in practice needs allocation failure or a broken libevent install. - The incorrect 2.1.1 minimum dates from the Prometheus exporter's introduction. Users with 2.1.1–2.1.3 already got a configure error from the link test; only the message and docs were wrong. - No change for a matched install: stdout, `--version` and existing JSON and crash-report keys are byte-identical. The only additions are the four JSON keys and the two crash INFO lines. ## Interactions - **#812** (merged) edited the DEVELOPMENT.md prerequisites line, raising the core minimum to 2.0.11. The merge keeps this PR's line, and the "Supported libevent versions" section now says why `configure.ac` requires 2.0.11. The `configure.ac` hunks do not overlap: #812 touches the core/pthreads checks, this PR the Prometheus block. - **#545** also adds a libevent table to DEVELOPMENT.md ("Libevent versions", with CI coverage) and its own #535 read-size note. Whichever of #545/#823 lands second merges the two tables into one, keeping this PR's support status next to #545's CI coverage, and keeps a single #535 paragraph. - **#822** (libevent 2.2 TLS error access): no overlap. The `BEV_EVENT_ERROR` handler is untouched here, and this patch applies cleanly on top of #822's head. Refs #545, #532, #535 <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Touches TLS connection setup, fatal evthread initialization, and version-mismatch detection on every run; behavior changes for broken libevent installs and mismatched header/library pairs, but matched installs stay warning-free. > > **Overview** > Adds **build vs runtime provenance** for libevent and OpenSSL: startup stderr warnings when major series (or OpenSSL 3.x minor) mismatch, four new JSON `run information` keys, and crash-report INFO lines separating loaded libraries from compile-time headers. > > **Hardens libevent integration:** `evthread_use_pthreads()` failure is **fatal** after option parsing (`--help`/`--version` unchanged). `shard_connection::setup_event()` returns `bool` and replaces asserts on `SSL_new` / bufferevent creation with logged errors and connect failure; TLS socket cleanup follows **runtime** libevent version (leave socket open on ≤2.1.8 when unsafe to close). Protocol buffers are cleared when the bufferevent is freed. > > **Docs/configure:** Prometheus exporter minimum is documented and enforced as **libevent 2.1.4** (`evhttp_request_set_on_complete_cb`). DEVELOPMENT.md adds a supported-libevent table and related CI notes. > > **Tests:** `library_versions.h` unit tests, LD_PRELOAD `library_failure_injector.c`, and `test_library_provenance.py` cover warnings, JSON, evthread fatality, and bufferevent failure paths; crash-handler integration checks new INFO fields. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 9276de2. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> Co-Authored-By: Claude Opus 5.5 <[email protected]>
…873) Fixes #852. `test_cluster_slots_never_started_client_fails_at_deadline` fails intermittently in CI (run 40130526066). A cluster client whose CLUSTER SLOTS keeps failing never starts. With unlimited reconnects its last retry is clamped to fire exactly at the `--test-time` deadline (`next_reconnect_delay()`). When the timer fires, `run_deadline_passed()` checks the wall clock against the deadline with 1 ms of slack. libevent on Linux times events with a coarse monotonic clock (1–4 ms ticks), so the timer can fire more than 1 ms early by the wall clock. The check then says the run isn't over, and the retry reconnects and gets a good slot map. The client ends normally at its first reply, so memtier exits 0 instead of failing the run for a client that never started. #812 fixed when the retry is scheduled; this is about when it fires. **Fix** (`shard_connection.cpp/.h`, +16): - `next_reconnect_delay()` records the deadline it clamped a reconnect to. - When that reconnect timer fires, `run_deadline_passed()` treats that deadline as passed, whatever the wall clock says. Unchanged: - retries due well before the deadline - finite `--max-reconnect-attempts` (never clamped, #656) - runs whose deadline moved later, once the client started (the wall clock still decides) **Test:** `test_deadline_retry_fires_early()` in `tests/unit/reconnect_siblings_test.cpp`. It arms a clamped retry 100 ms before the deadline and runs the timer handler at once, standing in for a timer that fires early. It checks that the client doesn't reconnect and that the run fails. A second case checks that a retry 2 s before the deadline still reconnects. With master's `shard_connection` files the new test fails both checks; with the fix it passes. Results (ASan + UBSan, libevent 2.1.12, Ubuntu 24.04 container): - `test_cluster_slots_bad_reply.py` (cluster) passes 20/20 in each of 5 iterations. - Six reconnect-related test files pass 73/73 in cluster and in standalone mode. - `make check` passes, and clang-format is clean. The end-to-end flake doesn't reproduce locally: this machine's coarse clock ticks at 1 ms, which the existing slack covers. That's why the regression test is a unit test. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Changes when timed runs with unlimited reconnects stop relative to libevent timer firing; limited to deadline-clamped reconnect paths but affects run completion and exit status for never-started clients. > > **Overview** > Fixes intermittent CI failure (#852) where a never-started cluster client could exit successfully after `--test-time` because a deadline-clamped reconnect timer fired a few milliseconds early on Linux (libevent coarse monotonic clock) while `run_deadline_passed()` still saw the wall-clock deadline as active. > > **`shard_connection`** now records the `--test-time` deadline when `next_reconnect_delay()` clamps backoff to that deadline, copies it into a persistent “fired” timestamp when that reconnect timer runs, and treats the run deadline as passed in `run_deadline_passed()` once that fired timestamp is at or past the current run deadline—without relying on wall clock at fire time. Pending deadline state is cleared when reconnect timers are cancelled or `event_new` fails. > > Adds **`test_deadline_retry_fires_early()`** to simulate an early timer fire for a CLUSTER SLOTS retry clamped to the deadline and assert the run fails without reconnecting; a second case confirms retries well before the deadline still reconnect. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 74bfa4e. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> Co-Authored-By: Claude Opus 5.5 <[email protected]>
Problem
A worker delayed inside a callback can arm its final reconnect before the
--test-timedeadline. If that early retry completes a never-started client's setup, the client starts a full additional workload interval. PR806's plaintext CI observed a three-second run report six seconds and exit 0 instead of rejecting the incomplete client.Root cause
next_reconnect_delay()subtracts a fresh wall-clock sample from the deadline, whileevent_add()schedules relative to libevent's cached loop time. A delayed callback makes those origins disagree. The existing 1 ms deadline allowance cannot cover arbitrary worker descheduling.Fix
Use
event_base_gettimeofday_cached()for the remaining-delay calculation so it sharesevent_add()'s time origin, with the existing wall-clock read as an API-error fallback. Preserve the existing deadline decisions, bounded reconnect schedule, startup handling, request-count behavior and output contracts.Require libevent 2.0.11 or newer, consistently for core, OpenSSL and pthread packages and the build documentation. Upstream 2.0.10 returns monotonic uptime through this API on Linux; 2.0.11 fixed the API to return wall time (a459ef7, April 2011). The old library exposes no portable public conversion to the cached wall origin. Falling back to fresh wall time would restore the original bug; changing every event base's cache policy or reconstructing private clock selection would add broader behavior and portability changes. This explicitly drops 2.0.10 support rather than silently accepting incorrect deadline clamping. The default-on Prometheus exporter still requires 2.1.1.
The actual minimum-version build also exposed two existing #553 compatibility errors: its local connect-address pointer was unnecessarily const for the older API, and a unit used a newer invalid-socket macro. Keep the already-mutable address pointer mutable, and compare the unit's listener against
static_cast<evutil_socket_t>(-1). No casts away from const, test removal or socket-policy changes.Tests
Affected versions
Unreleased master only, introduced by
0bb7fc5aa5222744f11d82f0935d5d78dabc3495(#666); no release tag contains that commit. The two minimum-build compatibility details were introduced by unreleasedcae16bf6(#553). The dependency floor changes from 2.0.10 to 2.0.11; users building with the older library must upgrade it.Fixes #811.
🤖 Generated with Claude Code
Note
Medium Risk
Changes timed-run shutdown and reconnect scheduling against
--test-time, and drops libevent 2.0.10 support; behavior is well-tested but affects core worker lifecycle.Overview
Fixes deadline-clamped reconnect timers firing too early when a worker spends time inside a libevent callback:
next_reconnect_delay()now usesevent_base_gettimeofday_cached()(same time origin asevent_add()) instead of a fresh wall clock, withgettimeofday()only if the cached read fails.Raises the libevent floor to ≥2.0.11 in
configure.acand docs, because 2.0.10’s cached time was monotonic on Linux and cannot be aligned with the benchmark’s wall-clock--test-timedeadline. Small build/test fixes: mutable connectsockaddrfor older APIs, invalid-socket check viastatic_cast<evutil_socket_t>(-1).Adds a unit regression in
reconnect_siblings_test.cppthat delays inside an event callback and asserts deadline clamping is unchanged.Reviewed by Cursor Bugbot for commit cf0591d. Bugbot is set up for automated code reviews on this repo. Configure here.