Skip to content

fix: free a lock before release() returns - #15

Merged
Shinrai merged 1 commit into
nextfrom
fix/release-mutex-before-return
Nov 28, 2026
Merged

Shinrai merged 1 commit into
nextfrom
fix/release-mutex-before-return

Conversation

@cldmv-bot

@cldmv-bot cldmv-bot Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

release() and a holder's close() took the shared takeover mutex before they returned, because the abstract socket binds synchronously, and it was only let go once the event loop ran again. A process that released and then blocked (a spawnSync of the next process that wants the lock) kept the mutex bound, and that child failed with MUTEX_TIMEOUT after 10 s.

close() now stops listening synchronously, so the folder is free as soon as release()/close() returns. It takes the mutex only after its first await, to clean up the socket file. Two follow-on fixes:

  • The file can now be taken over before that cleanup, and the new socket usually reuses the old inode number, so cleanup only removes the file when nothing answers on it.
  • A probe that hits a listener closing with the connection still queued gets ECONNRESET, which now counts as dead. It used to count as unknown, which made a contender throw UNKNOWN_STATE.

Tests: release or close without waiting, then spawnSync a child that takes the same folder. That failed with MUTEX_TIMEOUT before this change and passes now. There's also a test that a takeover before cleanup keeps the newer socket. In k3s-test with the PID-namespace suite on, everything passes: lint, format, types, build, all 114 tests, test:cjs and test:dist, and the 6-process mutual-exclusion test 8 times in a row.

Resolves #6

release() and a holder's close() took the shared takeover mutex before they
returned: the abstract socket binds synchronously, and it was only let go
once the event loop ran again. A process that released and then blocked,
such as a spawnSync of the next process that wants the same lock, kept the
mutex bound the whole time, and that child failed with MUTEX_TIMEOUT after
10 s.

close() now stops listening first, synchronously, so the folder is free
the moment release() or close() returns, and takes the mutex only after
its first await to clean up the socket file. Because the file can now be
taken over before that cleanup runs, and the new socket often reuses the
old inode number, the cleanup only removes it when nothing answers on it.
A probe that hits a listener closing with the connection still queued
gets ECONNRESET, which now counts as dead instead of unknown.

New tests release or close without waiting, spawnSync a child that takes
the same folder, and expect it to get it straight away; a third checks a
takeover before cleanup keeps the newer socket.

Resolves #6
@cldmv-bot cldmv-bot Bot added ! fix → next v4 flow: fix contributor PR targeting the next integration branch area: core Touches core library / runtime source code area: tests Touches test files, fixtures, or test infrastructure labels Nov 28, 2026
@Shinrai
Shinrai merged commit 160e7c8 into next Nov 28, 2026
37 checks passed
@cldmv-bot
cldmv-bot Bot deleted the fix/release-mutex-before-return branch November 28, 2026 09:24
Shinrai added a commit that referenced this pull request Nov 28, 2026
Follow-up to #15. next's CI failed coverage: branch coverage 99.27%
against the 100% threshold, on the cleanup line in `src/alive.mjs`. I
had run `npm test` in k3s-test, not the `ci:coverage` gate.

The uncovered branch was a newer holder answering during cleanup. With
#15, another process can take the folder over between `close()` stopping
and its cleanup, and its socket usually reuses the old inode number. So
the inode check couldn't tell the two apart, and the liveness probe was
what actually protected the newer socket. The cleanup now decides by
liveness alone: it removes the file only when nothing answers on it. A
live socket is never touched, and a dead one is fair to remove whoever
left it. The stored identity is gone.

A new test holds the cleanup back with the mutex mock until another
holder has taken the folder over, and checks the newer holder is still
alive. In k3s-test:
- `build:ci` and `ci:coverage` pass, at 100% on every metric;
- the full suite with the PID-namespace tests passed 115/115 five times
in a row.
cldmv-bot Bot added a commit that referenced this pull request Nov 28, 2026
# `@cldmv/lib-process` v0.1.1 Changelog

**Release Date**: November 2026
**Release Type**: Patch
**Branch**: `next` → `master`

---

## Overview

A lock is now free the moment `release()` returns. Before, a process that released a lock and then blocked, for example on a `spawnSync` of the next process that wants the same lock, left that process waiting on an internal mutex until it failed with `ERR_LIB_PROCESS_MUTEX_TIMEOUT` after 10 seconds. The same applies to a holder's `close()`.

The CommonJS entry is now the generic shim shared by the CLDMV libraries, with `require()` returning the same module object as before. The rest is CI and dev-tooling housekeeping.

---

## 🐛 Bug Fixes

### `release()` and `close()` free the folder before they return ([#15](#15), fixes [#6](#6))

`release()` and `close()` took the short cross-process mutex that serializes takeovers before they returned. The mutex is an abstract socket, which binds synchronously, and it was only let go once the event loop ran again. A caller that released without waiting and then blocked kept the mutex bound, so the next process timed out on it.

Now `close()` stops listening first, synchronously, so the folder is free as soon as `release()` or `close()` returns, even if the caller then blocks. The socket file is cleaned up afterwards under the mutex. Another process can take the folder over before that cleanup runs, and its socket often gets the same inode number, so the cleanup decides by liveness instead: it only removes the file when nothing answers on it, and a newer holder's live socket is never touched ([#17](#17)). A probe that reaches a listener just as it closes now counts as dead (`ECONNRESET`) instead of throwing `ERR_LIB_PROCESS_UNKNOWN_STATE`.

The returned promise still settles once the cleanup is done, so `await held.release()` behaves as before. New tests release without waiting, `spawnSync` a child that takes the same folder, and expect it to get the folder at once, and hold the cleanup back until another holder has taken over to check the newer socket survives; the multi-process mutual-exclusion test still holds.

---

## 📦 Packaging

### The CommonJS entry is the generic shim ([#12](#12))

`src/index.cjs`, published as `dist/index.cjs`, is now the shim every CLDMV library ships: a Node.js version guard, then `require("./index.mjs")`. lib-process exports several values, so `require("@cldmv/lib-process")` returns the module object, exactly as before. The CJS test checks its keys and values against the ESM entry in `src/` and `dist/`.

---

## 🔧 CI & tooling

- `ci.yml` adopts the CLDMV/.github v4.46.0 concurrency block, so superseded runs on `next`, `hotfixes` and the release PR are cancelled and only release pushes always finish ([#13](#13), fixes [#11](#11)).
- `ci.yml` is synced with the v4.43.0 template ([#8](#8)).
- `dependabot.yml` takes the npm groups from the CLDMV/.github v4.47.0 template, so the CLDMV dev tools arrive in one PR ([#14](#14)).

## 📚 Documentation

- **NEW:** [docs/changelog/v0/v0.1.1.md](https://github.com/CLDMV/lib-process/blob/v0.1.1/docs/changelog/v0/v0.1.1.md), this changelog.
- The `release()` and `close()` docs say the folder is free as soon as the call returns ([#15](#15)).
- README What's New promoted to v0.1.1.

## 🔧 Dependencies

Development dependencies only; lib-process still has no runtime dependencies.

- `@cldmv/vitest-runner` 1.6.1 → 1.11.3, with `@cldmv/fix-headers` 2.6.0, `@cldmv/jsonv` and `@cldmv/prettier-plugin-jsonv` 1.2.3, `eslint` 10.16.0 and `vitest` 5.1.2 ([#14](#14)).

---

## Upgrade notes

- No breaking changes. Code that awaits `release()` or `close()` behaves as in [v0.1.0](https://github.com/CLDMV/lib-process/blob/v0.1.1/docs/changelog/v0/v0.1.0.md); code that doesn't await them now frees the lock straight away instead of holding up the next process.



<details>
<summary>👥 Contributors</summary>

- @Shinrai

</details>

---

<!-- coverage-start -->

![coverage](https://img.shields.io/badge/coverage-100.0%25-brightgreen?style=for-the-badge&logo=vitest&logoColor=white)

| Metric | Coverage |
|--------|----------|
| Statements | 100.0% |
| Branches   | 100.0% |
| Functions  | 100.0% |
| Lines      | 100.0% |

*Avg: **100.0%** · `e726033` · Node lts/**

<!-- coverage-end -->

<!-- co-authors -->

Co-authored-by: Shinrai <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: core Touches core library / runtime source code area: tests Touches test files, fixtures, or test infrastructure ! fix → next v4 flow: fix contributor PR targeting the next integration branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant