http_client: don't arm the timeout of a request that has already failed #184

Merged
vitalif merged 1 commits from koolkhel/vitastor:http-stray-timer into master 2026-11-27 13:38:29 +03:00
Contributor

Description

A request can fail inside http_request():

  • connect() refused at once (ENETUNREACH, EHOSTUNREACH, a blackhole or prohibit route);
  • socket() failing (EMFILE);
  • TLS setup failing.

In those cases its callback has already run when send_request() arms the timeout, and the timer was armed anyway. Nothing cleared it: the next request overwrote timeout_id. Two things could then happen:

  • False timeout. The timer later failed the client's next request with "HTTP request timed out", although that request's own timeout hadn't expired. For etcd_call() that also means a switch to the next etcd address. A txn cut off this way may still be applied by etcd after the retry's newer one.
  • Use after free. If the failed request's callback had destroyed the client, as etcd_call_oneshot() does, the timer ran on freed memory.

The fix arms the timer only while the request is still in flight. Then the client still holds the request's callback and no other timer is armed. A request that failed synchronously has already cleared its callback. A queued request that next_request() started from that failure has armed its own timer.

Tested: new test_http_client. A request to 224.0.0.1:1 fails at once with ENETUNREACH. A second request on the same client must then end only by its own 5 s timeout. Without the fix the leftover 1 s timer fails it at 1 s; with the fix the test passes. The use-after-free variant wasn't run under ASan.

Contributor Name

Yury Luneff (MIND Software LLC, https://mindsw.io)

CLA

### Description A request can fail inside `http_request()`: - `connect()` refused at once (ENETUNREACH, EHOSTUNREACH, a `blackhole` or `prohibit` route); - `socket()` failing (EMFILE); - TLS setup failing. In those cases its callback has already run when `send_request()` arms the timeout, and the timer was armed anyway. Nothing cleared it: the next request overwrote `timeout_id`. Two things could then happen: - **False timeout.** The timer later failed the client's next request with "HTTP request timed out", although that request's own timeout hadn't expired. For `etcd_call()` that also means a switch to the next etcd address. A txn cut off this way may still be applied by etcd after the retry's newer one. - **Use after free.** If the failed request's callback had destroyed the client, as `etcd_call_oneshot()` does, the timer ran on freed memory. The fix arms the timer only while the request is still in flight. Then the client still holds the request's callback and no other timer is armed. A request that failed synchronously has already cleared its callback. A queued request that `next_request()` started from that failure has armed its own timer. Tested: new `test_http_client`. A request to `224.0.0.1:1` fails at once with ENETUNREACH. A second request on the same client must then end only by its own 5 s timeout. Without the fix the leftover 1 s timer fails it at 1 s; with the fix the test passes. The use-after-free variant wasn't run under ASan. ### Contributor Name Yury Luneff (MIND Software LLC, https://mindsw.io) ### CLA - [x] I accept Vitastor CLA agreement: https://git.yourcmc.ru/vitalif/vitastor/src/branch/master/CLA-en.md
koolkhel added 1 commit 2026-11-25 03:19:50 +03:00
A request can fail inside http_request(): connect() refused at once
(ENETUNREACH), socket() failing (EMFILE), TLS setup failing. Its callback
has then already run when send_request() arms the timeout, and the timer
was armed anyway. Nothing cleared it: the next request overwrote its id.
It later failed the client's next request with a false "HTTP request
timed out", or ran on a freed client when the callback had destroyed it,
as etcd_call_oneshot() does. Arm the timer only while the request is
still in flight.
vitalif merged commit 1b4f036b4e into master 2026-11-27 13:38:29 +03:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: vitalif/vitastor#184
No description provided.