Skip to content

fix(client-node): stop an aborted waiting request from destroying a socket in use - #1041

Merged
ccoltx merged 3 commits into
ClickHouse:mainfrom
vvo:fix-idle-timer-on-repeated-free
Oct 9, 2026
Merged

ccoltx merged 3 commits into
ClickHouse:mainfrom
vvo:fix-idle-timer-on-repeated-free

Conversation

@vvo

@vvo vvo commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

tl;dr; stops socket hang up errors after an aborted request, by clearing the previous idle-socket timer when a socket is released a second time.

Problem

  • When all max_open_connections sockets are busy, new requests wait for one. If a waiting request is aborted, Node.js releases the next free socket twice: once for the request that finished, once for the aborted one.
  • Each release starts an idle timer and overwrites idle_timeout_handle without clearing the previous timer. The first timer fires idle_socket_ttl later and destroys the socket while another request is using it.

Solution

  • We clear the pending timer before starting a new one. Idle sockets still close after the TTL.
Implementation details
  • Fixes Aborting a request that waits for a socket destroys that socket under a later request (socket hang up) #1040, which has a standalone repro.
  • The second release comes from onSocketNT in Node's _http_client.js, which emits free on the socket it hands to an already-destroyed request.
  • The new test uses a plain HTTP server and max_open_connections: 1: one request holds the socket, a waiting request is aborted, and a third request runs past the release plus the TTL. It fails on main with socket hang up and passes with the fix.
  • Stress run (400 requests, 3 sockets, 30% aborted at random): 18 to 19 hang-ups per run before, 0 after.
  • We hit this on Vercel Functions, where one process serves many concurrent requests: thousands of socket hang up errors per hour once requests started waiting for sockets.
  • Includes the test and a changelog entry for @clickhouse/client.

Copilot AI balanced review requested due to automatic review settings October 7, 2026 16:03
@vvo
vvo marked this pull request as draft October 7, 2026 16:03

Copilot AI 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.

🟡 Changes recommended

The new changelog version does not match the package metadata.

1 open finding
What changed in this PR

Fixes orphaned idle-socket timers when queued requests are aborted.

Changes:

  • Clears the previous timer on repeated free events.
  • Adds a regression test and changelog entry.
File Description
socket_pool.ts Clears stale idle timers.
node_abort_queued_request.test.ts Covers the socket-reuse regression.
CHANGELOG.md Documents the fix.

🧠 Review effort: Balanced


💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread packages/client-node/CHANGELOG.md Resolved
@vvo vvo changed the title fix(client-node): clear the previous idle socket timer on a repeated free event fix(client-node): stop an aborted waiting request from destroying a socket in use Oct 7, 2026
@vvo
vvo marked this pull request as ready for review October 8, 2026 10:30
@ccoltx
ccoltx merged commit c2d69e7 into ClickHouse:main Oct 9, 2026
59 checks passed
@peter-leonov-ch peter-leonov-ch mentioned this pull request Oct 9, 2026
kavirajk pushed a commit that referenced this pull request Oct 22, 2026
…rly (#1076)

## Summary

Runs the ClickHouse Cloud integration tests after a PR is merged, or on
request, instead of on every push to every PR. Also runs them one job at
a time. This follows what
[clickhouse-go](https://github.com/ClickHouse/clickhouse-go/blob/main/.github/workflows/run-tests-cloud.yml)
does against the same Cloud service.

### Why

The Cloud jobs use one ClickHouse Cloud service, which is shared with
other client repositories (clickhouse-go uses the same
`INTEGRATIONS_TEAM_TESTS_CLOUD_HOST_SMT_PROD` secret). On Cloud, every
test file creates its own database, and `clean-up.yml` only drops
databases older than 60 minutes.

Each push to a PR started six Cloud jobs at once. Pushing a stack of
eight PRs (#1065 to #1072) twice within 15 minutes filled the service:

```
Too many databases. The limit (max_database_num_to_throw) is set to 2800,
the current number of databases is 2800. (code 725)
```

From then on, every Cloud job, including #1075's, failed during setup
before running any tests. This likely affects the other repositories
that use the same service too.

### What

- **New `tests-cloud.yml`** with the Node.js and Web Cloud jobs, which
are removed from `tests-node.yml` and `tests-web.yml`. It runs:
- after a merge (push to `main` or `release`, with the same path filters
as before), on the daily schedule, and on `workflow_dispatch`;
- on a PR only when a maintainer adds the **`tests:run-cloud`** label. A
final job removes the label, so adding it again runs the tests again.
- **One Cloud job at a time across the repository:**
- one workflow-level concurrency group, `tests-cloud`, with
`cancel-in-progress: false`, so a whole run holds the lock and other
runs wait. Of the waiting runs, only the newest is kept.
- within a run, `max-parallel: 1` runs the matrix jobs one by one, and
the Web jobs wait for the Node.js jobs.
- runs whose jobs will be skipped, for example a PR that got a different
label, get a group of their own (`tests-cloud-skipped-<run id>`), so
they can't replace a waiting Cloud run.
- **`examples.yml` passes the Cloud credentials only after a merge and
on manual runs.** On PRs they're empty, so the `create_table_cloud`
example (Node.js and Web) skips itself, which it already did when the
credentials were missing. Before this, each of the 4 examples jobs of
every PR hit the Cloud service. With the lexer stack's PRs running at
once, that example timed out in 28 jobs.
- **`clean-up.yml` runs hourly instead of on every push.** It used to
run on every push to every branch, and a new push cancelled one still in
progress. Dropping databases one by one takes a while, so a burst of
pushes kept restarting it. It now runs at :17 every hour, which also
puts a run before the 09:00 scheduled Cloud tests, and on demand
(`workflow_dispatch`). Only one runs at a time, and a running cleanup is
never cancelled.
- **Fork and Dependabot PRs are still skipped**, as before, because
Cloud secrets aren't available to them. It uses `pull_request`, not
`pull_request_target`, so a PR's code never runs with secrets unless it
comes from a branch in this repository.
- **`npm ci` instead of `npm install`** in the new workflow, so
dependencies are installed exactly as locked, verified by hash. This
resolves the Scorecard Pinned-Dependencies alerts on these two steps.
The other workflows still use `npm install`, so their existing alerts
are unchanged.
- **`codecov.yml`:** `after_n_builds` goes from 7 to 5, the uploads
every PR gets. With 7, Codecov never posted a status on fork PRs (for
example #1041 and #1024), which skip the Cloud jobs, and with this
change it wouldn't post on most PRs. The cloud flags are carried forward
(`carryforward: true` was already set).

### Trade-offs

- **Cloud regressions show up after merge** (on `main`), unless the
label is added before merging.
- **A Cloud run takes longer** with jobs one at a time: 4 Node.js
versions and then 2 browsers, about 25–30 minutes instead of about 5.
- **The `tests:run-cloud` label is new.** I've created it in the
repository.

## Testing

- All workflow and Codecov YAML parses, and Prettier passes.
`actionlint` wasn't available locally.
- The new workflow can only really be checked on GitHub: after merge
(push to `main`), and by adding `tests:run-cloud` to a PR once the Cloud
service has been cleaned up.

## Checklist

- [x] CI only; no changelog entry needed.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

Aborting a request that waits for a socket destroys that socket under a later request (socket hang up)

4 participants