Skip to content

Handle libevent 2.1.8's error callback from a failing connect; test on Rocky Linux 8 - #855

Merged
fcostaoliveira merged 5 commits into
masterfrom
fix/libevent218-sync-connect-error
Oct 26, 2026
Merged

fcostaoliveira merged 5 commits into
masterfrom
fix/libevent218-sync-connect-error

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

RHEL 8 ships libevent 2.1.8, the oldest release memtier is known to work with. CI never ran it. Running the standalone suite on Rocky Linux 8 (libevent 2.1.8, OpenSSL 1.1.1k, GCC 8, Redis 6.2) gave 12 failures.

10 in test_unix_connect_backlog, with SIGSEGV. libevent 2.1.8 runs the bufferevent error callback from inside a failing bufferevent_socket_connect() (freesock: calls bufferevent_run_eventcb_(bev, BEV_EVENT_ERROR, 0)), before it returns -1. 2.1.9 stopped doing that (1dde74ef, "bufferevent_socket_connect{,_hostname}() missing event callback and use ret code"). memtier therefore handled the failure twice:

  1. handle_event() logged Connection error: Resource temporarily unavailable, then reconnected or ended the thread, which freed the bufferevent.
  2. connect() then handled the -1 itself. For a full Unix socket backlog (EAGAIN) that is bufferevent_disable(m_bev, ...) on the freed bufferevent:
memtier_benchmark crashed by signal: 11
/lib64/libevent-2.1.so.6(bufferevent_disable+0x14)
shard_connection::connect(connect_info*) shard_connection.cpp:692

The Unix backlog retry (handle_unix_connect_retry()) calls bufferevent_socket_connect() directly and had the same problem.

1 in test_cli_validation_reconnect_backoff. The same double handling counted a failed reconnect twice.

test_F16_ipv6. This one is environmental. evhttp resolves the bind address with AI_ADDRCONFIG, which rejects ::1 in a container with no non-loopback IPv6 address. The skip guard only tried bind().

Fix

  • socket_connect() wraps bufferevent_socket_connect() for both callers. While it runs, handle_event() only records the error, and the caller handles the failure once, as with libevent 2.1.9 and newer. Deferred callbacks (BEV_OPT_DEFER_CALLBACKS, e.g. an immediate refusal) run later from the loop and are unaffected. The Windows path, which goes through bufferevent_connect_nonblocking(), sets the same guard.
  • test_F16_ipv6 now also skips when getaddrinfo("::1", AI_PASSIVE | AI_ADDRCONFIG) fails, which is the lookup evhttp makes.
  • New CI job Test Rocky Linux 8 (libevent 2.1.8), plaintext and TLS. It runs a rockylinux:8 container with the distro's libevent, OpenSSL and GCC, Redis 6.2 from AppStream, and Python 3.11. It checks --version reports libevent=2.1.8-stable, runs make check, then runs the standalone suite.

Testing

  • Rocky Linux 8, locally, with the same steps as the new CI job: plaintext 869/869 and TLS 869/869 pass. Before the fix, plaintext had 12 failures. test_unix_connect_backlog also passed 3 out of 3 repeated runs.
  • Ubuntu 24.04, libevent 2.1.12, ASan + UBSan: test_unix_connect_backlog.py, test_cli_validation_reconnect_backoff.py and test_reconnections.py pass 51/51.
  • Not covered: the cluster suite on 2.1.8 is not part of the new job.
  • Maintainer decision: this job is the only check that catches the fix. Every other cell runs libevent 2.1.12 or newer, and make check passes without the fix. Consider making it required.

Related: #823 documents 2.1.8 as the oldest known-working release ("CI does not build it yet"). This PR adds that CI job. Whichever of the two lands second updates that row.

🤖 Generated with Claude Code


Note

Medium Risk
Changes core connection setup in shard_connection for all POSIX connects; behavior is version-specific but affects reconnect/backlog paths on every platform.

Overview
Fixes double-handling of synchronous connect failures on libevent 2.1.8 (RHEL 8): when bufferevent_socket_connect() fails, that release invokes the error callback before returning -1, which previously drove reconnect/teardown and then left connect() touching a freed bufferevent (SIGSEGV on Unix backlog retries and similar paths).

socket_connect() wraps the libevent call and, while it runs, handle_event() only stashes the socket error in m_socket_connect_error so connect() / handle_unix_connect_retry() handle the failure once, matching 2.1.9+ behavior. Comments note BEV_OPT_DEFER_CALLBACKS is intentionally not used for this contract.

Adds CI job test-rockylinux-8 (Rocky Linux 8, libevent 2.1.8, plaintext + TLS): make check and the standalone integration suite. DEVELOPMENT.md documents that coverage.

test_F16_ipv6 skip guard now mirrors evhttp by probing getaddrinfo("::1", …, AI_ADDRCONFIG), so IPv6 Prometheus tests skip in Docker without non-loopback IPv6.

Reviewed by Cursor Bugbot for commit 63a43af. Bugbot is set up for automated code reviews on this repo. Configure here.

libevent 2.1.8 and older run the bufferevent error callback from inside a
failing bufferevent_socket_connect(), before it returns -1; 2.1.9 stopped
doing that (1dde74ef). memtier handled the failure twice: handle_event()
logged it and reconnected or ended the thread, freeing the bufferevent,
and connect() then handled the -1 as well. On RHEL 8 (libevent 2.1.8) a
full Unix socket listen queue (EAGAIN) crashed with SIGSEGV in
bufferevent_disable() on the freed bufferevent, and a failed reconnect
was counted twice.

Both calls (connect() and the Unix socket backlog retry) now go through
socket_connect(): while it is inside bufferevent_socket_connect(),
handle_event() only records the error, and the caller handles the
failure once, as with newer libevent. Deferred callbacks run later from the event loop
and are unaffected.

CI never ran libevent 2.1.8, the oldest release DEVELOPMENT.md lists as
working: add a Rocky Linux 8 job (libevent 2.1.8, OpenSSL 1.1.1k, GCC 8,
Redis 6.2) running make check and the standalone suite, plaintext and
TLS. test_F16_ipv6 now skips where getaddrinfo() with AI_ADDRCONFIG, as
evhttp resolves the bind address, rejects ::1 (e.g. a container without
a non-loopback IPv6 address).

On Rocky Linux 8 the standalone plaintext suite had 12 failures: 10 in
test_unix_connect_backlog, 1 in test_cli_validation_reconnect_backoff
and test_F16_ipv6. Those files now pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

🤖 Automated first-pass review — a human maintainer's review is still required before merge.

Thanks for this. The write-up of the 2.1.8 freesock: behaviour made the fix easy to check. I went through the shard_connection changes and the new CI job, and have a few comments below.

  • I followed the guard against 2.1.8's connect path. freesock: always returns -1 after it runs the event callback, so as far as I can tell m_socket_connect_error can't get set while socket_connect() returns 0. That means no error is silently swallowed. Capturing EVUTIL_SOCKET_ERROR() inside handle_event() also looks right, since that's the last point where errno still holds the connect error. The early return only fires on BEV_EVENT_ERROR, which looks narrow enough to me.
  • The guard only works because neither bufferevent uses BEV_OPT_DEFER_CALLBACKS. Right now a comment in setup_event() is the only thing protecting that. If someone adds the flag later, I think every cell except the Rocky one stays green and the double handling comes back without anyone noticing. I agree with what you wrote in the description: this job is effectively the only regression test for the fix. Making it a required check seems like the right call, otherwise it's easy to merge a red Rocky run without noticing.
  • Just checking on test_F16_ipv6: did you confirm it still runs rather than skips on the existing Ubuntu cells? If the hosted runners have no non-loopback IPv6, the AI_ADDRCONFIG guard would quietly skip the test everywhere. That seems unlikely, since evhttp would have failed there before this PR too, but the job logs would settle it.
  • Nitpicking: the new job runs both TLS cells against the full standalone suite with a 40-minute timeout, which adds noticeably to CI time. That's fine by me if the goal is to make it required. It's worth deciding knowingly, though.

You already flagged that the cluster suite on 2.1.8 isn't covered. That's fine as a follow-up, I don't think it should block this.

The core fix looks solid and small to me. I'd mainly like a maintainer to decide whether the Rocky job becomes required before this merges.

- handle_event() returns early inside socket_connect() only for an
  error event; any other event is handled as usual, should a libevent
  ever deliver one synchronously.
- The Windows path passes libevent no address, so libevent cannot fail
  that connect synchronously: say so instead of setting the guard.
- Say "before 2.1.9" rather than "2.1.8 and older" (configure accepts
  2.0.11), and describe RHEL 8's libevent in the CI comment without
  pointing at a DEVELOPMENT.md line that master does not have yet.
- test_F16_ipv6: socket.gaierror is an OSError.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks. Pushed cb1cbe0:

  • Filtering events. The guard now returns early only for BEV_EVENT_ERROR. Any other event goes through handle_event() as usual, and connect() has already set conn_in_progress before the call. I read bufferevent_socket_connect() in 2.0.11, 2.0.22, 2.1.8, 2.1.9, 2.1.13 and 2.2.2. The only callback during the call is the freesock: error on 2.0.x and 2.1.8, and it fires exactly when the call returns -1. CONNECTED is never synchronous, and an immediate refusal is deferred.
  • Windows. It was belt-and-suspenders. bufferevent_connect_nonblocking() connects the socket itself and passes libevent NULL, so libevent can't fail that connect synchronously. I removed the guard there and left a comment saying why.
  • Coverage. Yes, the existing test_unix_connect_backlog and test_cli_validation_reconnect_backoff failures are what pin the fix. On Rocky 8, master fails 10/10 and 11/12, and the PR passes all of them. A 2.1.8 cluster cell can be a follow-up. An independent review with a fake CLUSTER SLOTS server pointing half the slots at an unreachable node saw master on 2.1.8 count each failed node connect twice (6 errors). The PR counts 5, the same as 2.1.12.
  • except OSError. Done.

Also from the reviews: the comments say "before 2.1.9", since configure accepts 2.0.11 and 2.0.x behaves like 2.1.8. The CI comment no longer cites a DEVELOPMENT.md line that only #823 adds.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks. Pushed 71ff40a:

  1. Deferred callbacks. Added the note in setup_event(): neither bufferevent may use BEV_OPT_DEFER_CALLBACKS, because socket_connect() relies on the in-call error callback. CI also catches that change. A review mutant that adds BEV_OPT_DEFER_CALLBACKS to bufferevent_socket_new() fails 7 of the 10 test_unix_connect_backlog tests on Rocky 8, with the double handling back.
  2. F16 on Ubuntu. It still runs. It showed [PASS] in the Ubuntu standalone job on 14d6b29 (job 121006139292), and except OSError behaves the same. You're right that evhttp goes through evutil_getaddrinfo, not glibc's AI_ADDRCONFIG directly. To check that the guard predicts evhttp, the TEST review compared it with evhttp's own ::1 bind on libevent 2.1.8 and 2.1.12, with IPv6 off, link-local only, a local-only address and a global address. The guard and evhttp agreed in every case. F16 now skips only in the Rocky containers, where evhttp can't bind ::1 either.
  3. TLS leg per PR. The two Rocky cells took 22m10s and 21m52s in the test step, about the same as the existing standalone cells, and they run in parallel with them. I'd keep TLS per PR: the libevent 2.1.8 TLS connect path is where the double handling also crashed, and Report libevent/OpenSSL build vs runtime versions and harden libevent failures #823 adds 2.1.8-specific TLS socket-ownership code that only this job runs. Dropping it to push-only is a one-line change if CI time matters more. That's the maintainers' call.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks.

  • m_socket_connect_error = 0 on Windows. Confirmed. It's only there so the shared ret == -1 branch doesn't read a stale value from an earlier socket_connect() call on that connection. With a NULL address, bufferevent_socket_connect() never reaches connect()/freesock. That's true on 2.1.8, 2.1.13 and 2.2.2, including the IOCP path, which memtier never uses (every base comes from event_base_new()).
  • F16 on the Ubuntu cells. It runs and passes there. It reported [PASS] in the Ubuntu standalone job on 14d6b29 (job 121006139292), and the guard is unchanged in substance since then. It skips only in the Rocky 8 containers, where evhttp can't bind ::1 either.
  • Required check. Agreed, that's for whoever owns the CI config. The description lists it under "Maintainer decision".

fcostaoliveira and others added 2 commits October 26, 2026 09:51
Resolve the comment-only conflict in setup_event() by keeping both notes,
and say in DEVELOPMENT.md that CI now runs 2.1.8 (test-rockylinux-8).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira merged commit 60ec3f0 into master Oct 26, 2026
68 of 69 checks passed
fcostaoliveira added a commit that referenced this pull request Oct 26, 2026
Keep both sides in shard_connection: #855's socket_connect members and
NULL check, then this PR's read tracking. DEVELOPMENT.md (from #545 and
#823) now says memtier reads past 2.1's 4 KiB cap on plaintext.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 26, 2026
Fixes #856.

When the Prometheus exporter could not resolve its bind address, the error ended in "Success":

```
error: prometheus exporter: failed to bind ::1:0: Success
```

`evhttp_bind_socket_with_handle()` resolves the address with `getaddrinfo()`. When that fails (for example `AI_ADDRCONFIG` rejecting `::1` on a host without a non-loopback IPv6 address), no socket error is set, so we printed errno 0. Changes, all in `prometheus_exporter::start()`:

- Clear the socket error before the call.
- Print the error only when it is non-zero. Otherwise print "address could not be resolved or used".
- Bracket IPv6 addresses, using the same `find(':')` check as the "listening on" line. The message is now `failed to bind [::1]:0: address could not be resolved or used`, or `failed to bind [2001:db8::1]:0: Cannot assign requested address` on a host with IPv6.

**Test:** `test_F16b_bind_failure_message` in `tests/test_prometheus.py` binds to `2001:db8::1`, a documentation address that is never local, so the bind fails on every host. On hosts without IPv6 it fails at `getaddrinfo()`, the path that printed "Success". On GitHub runners `bind()` fails with `EADDRNOTAVAIL`. The test checks for exit code 1, a bracketed address, and a message that doesn't end in "Success". It also tries `::1` and skips that case if `::1` binds. (A `.invalid` hostname can't be used, because `--prometheus-bind-addr` rejects hostnames while parsing options.)

CI coverage: GitHub runners have IPv6, so in CI `2001:db8::1` fails at `bind()` and `::1` binds. CI therefore checks the brackets and the real `bind()` reason, but not the no-errno fallback. That path was verified in IPv6-less containers (below).

Results (Ubuntu 24.04 container, libevent 2.1.12):
- Master's `prometheus_exporter.cpp` with the new test fails 4 checks without IPv6, and fails the bracket check on an IPv6 Docker network.
- With the fix, the test passes in both. `make check` passes. `test_prometheus.py` passes, apart from `test_F16_ipv6` in the IPv6-less container (fixed by #855). clang-format is clean.


<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Low Risk**
> Startup-only error messaging and diagnostics; no change to metrics serving or bind behavior beyond clearer stderr output.
> 
> **Overview**
> Fixes misleading **"Success"** text when the Prometheus exporter cannot bind (#856). Before `evhttp_bind_socket_with_handle()`, the code now clears the libevent socket error so a stale errno is not reported. On failure it prints a **bracketed IPv6** address (matching the listen URL) and either the real socket error or **"address could not be resolved or used"** when `getaddrinfo()` fails without setting errno.
> 
> Adds **`test_F16b_bind_failure_message`**, which binds to `2001:db8::1` (and optionally checks `::1` if bind fails) to assert exit code 1, bracketed addresses, and no trailing "Success".
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 1df3df7. 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]>
fcostaoliveira added a commit that referenced this pull request Oct 26, 2026
Picks up 14 master commits, including the connect-failure changes in
#855 (libevent 2.1.8's error callback from a failing connect), #823
(hardened libevent failures), #822 (TLS error reporting) and the
libevent compatibility CI from #545. No conflicts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 27, 2026
Fixes the `Test Rocky Linux 8 (libevent 2.1.8, TLS=1)` job, which has failed on master since #855 landed (first failing run: 60ec3f0).

`test_bufferevent_creation_failure_fails_connection` (from #823) always expected memtier to close the socket after `bufferevent_openssl_socket_new()` fails. On a real libevent 2.1.8 or older with TLS, memtier deliberately leaves that socket open, because libevent may already have closed it (`libevent_ssl_failure_socket_is_ours()`). The neighbouring `test_tls_bufferevent_failure_leaves_socket_on_libevent_2_1_8` checks exactly that with a faked version. #823 and #855 merged one after the other, so the real-2.1.8 TLS job never ran this test before landing.

The test now reads the runtime libevent from `--version`. With TLS on libevent older than 2.1.9 it expects `left open on purpose` and the shim's `socket open` marker; otherwise it still expects `socket closed`. This is a test-only change, and memtier's behaviour is unchanged and correct.

Results for `test_library_provenance.py`:
- Rocky Linux 8 (libevent 2.1.8) with TLS: 7/7.
- Ubuntu 24.04 (libevent 2.1.12), ASan + UBSan, plain and TLS: 7/7 each.


<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Low Risk**
> Test-only change; memtier runtime behavior is unchanged and only assertions are version-aware.
> 
> **Overview**
> Fixes CI on **Rocky Linux 8** (real libevent 2.1.8 with TLS) by aligning `test_bufferevent_creation_failure_fails_connection` with memtier’s existing socket-handling rules.
> 
> That test used to always require the preload shim’s **`socket closed`** marker after a forced `bufferevent_*_socket_new()` failure. With **TLS** and **libevent older than 2.1.9**, memtier intentionally leaves the fd open and logs **`left open on purpose: libevent …`** because libevent may have already closed it. The test now parses the runtime libevent version from `--version` and branches: TLS + &lt; 2.1.9 expects the open-socket path; all other cases still expect **`socket closed`**. No application code changes.
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 8e30c4f. 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]>
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.

1 participant