fix(container): cache the in-flight promise of an app-scoped binding (GitHub #46)
CI / dco (pull_request) Successful in 5s
CI / runtimes (bun) (pull_request) Successful in 20s
CI / build (pull_request) Successful in 29s
CI / runtimes (edge) (pull_request) Successful in 19s
CI / runtimes (deno) (pull_request) Successful in 33s

Container.resolve marked an app-scoped binding resolved only after its
factory settled. Two resolutions that started before then both found
nothing cached and both ran the factory, so one token could open two
pools.

The binding now caches the promise as soon as the factory starts, and
every resolution returns that promise. Retry semantics are unchanged:
before, a rejected factory set nothing and the next resolution ran it
again. Now a rejection clears the cached promise, so the next
resolution still retries, and only the callers already waiting on the
failed attempt share its rejection. The factory runs inside an async
wrapper so a synchronous throw takes the same path.

createApp was not exposed to this in practice. bootProvider runs each
provider's runner once, inside the memoized boot, and registers the
finished value as a constant factory, and Container is not exported.
The fix makes the container's own guarantee hold instead of relying on
that.

Refs: GitHub #46
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-24 15:58:22 -05:00
1 parent 9f6f916063
commit 0c7b5b25b6
3 files changed
+67 -10

No files matched your search

+7
View File
@@ -260,6 +260,13 @@ npm treats versions as semver and semver forbids leading zeros.
### Fixed
- **An app-scoped binding runs its factory once, however many resolve it at once** (GitHub #46).
The internal container cached the value only after the factory settled, so two resolutions that
started before then each ran it. It now caches the pending promise. A rejection is still not
cached: the next resolution retries, as before, and the callers already waiting share that one
rejection. `createApp` was not affected in practice, because it runs each provider once during
the memoized boot and registers the finished value, but the container no longer depends on that.
- **`listen()` rejects when the port is taken** (#138). A bind failure such as `EADDRINUSE` or
`EACCES` arrives as the server's `error` event, which `listen()` did not handle: the promise
never settled, and the error surfaced as an unhandled one. It now rejects with that error. The