Skip to content

Commit b525afe

Browse files
docs(components): scope shutdown cleanup to what needs an explicit hook
Address review feedback on the new shutdown cleanup coverage. The section listed timers and open connections as things an application is responsible for tearing down. Node closes timers and sockets itself when a thread exits, so presenting them as the app's job is misleading. Reframe both the plugin-api.md section and the applications.md pointer around work that genuinely needs a hook: flushing buffered writes, deregistering from an external service, releasing a distributed lock, or closing a connection whose remote side expects a graceful goodbye. Also drop the incorrect justification for `scope.once()`. It claimed `once` avoids leaving a listener behind during a reload, but `Scope.close()` detaches its `'close'` listeners as part of closing and a reload builds a new Scope, so `on()` cannot strand a listener on a closed scope either. Recommend `once()` on the honest grounds that closing is a one-time lifecycle event. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 5c7acbd commit b525afe

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

reference/components/applications.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ harper deploy \
6868
6969
## Shutdown Cleanup
7070

71-
Applications that start background work — a service, a timer, a connection pool, a buffer that needs flushingshould tear it down when Harper stops or restarts. Restarts are frequent during local development, since `harper dev` restarts worker threads on every file change, and deploying with `restart=true` does the same on a running instance.
71+
Applications with work that must complete, or be acknowledged outside the process, before a thread exits — buffered writes to flush, a registration to withdraw from an external service, a distributed lock to releaseneed a hook to do it when Harper stops or restarts. Restarts are frequent during local development, since `harper dev` restarts worker threads on every file change, and deploying with `restart=true` does the same on a running instance.
7272

7373
Harper signals this by calling `scope.close()` on each worker thread, which emits a `'close'` event on the plugin API [`Scope`](./plugin-api.md#class-scope). Listen for it to run cleanup:
7474

reference/components/plugin-api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ Function signature for the `'all'` event handler passed to `scope.handleEntry()`
387387

388388
Harper calls `scope.close()` on every application scope when a worker thread shuts down, which emits the [`'close'`](#events) event. This happens both when Harper is stopping and on a graceful restart — including the automatic worker restarts triggered by `harper dev` file watching and by the `restart` operation.
389389

390-
Listen for `'close'` to release anything the plugin or application acquired at load time: background services, timers, open connections, or buffered work that needs flushing.
390+
Listen for `'close'` to finish work that must complete, or be acknowledged outside the process, before the thread goes away: flushing buffered writes, deregistering from an external service or registry, releasing a distributed lock or lease, or closing a connection whose remote side expects a graceful goodbye. Node tears down timers and sockets on its own when a thread exits, so purely in-process resources need no `'close'` listener.
391391

392392
```js
393393
export function handleApplication(scope) {
@@ -398,7 +398,7 @@ export function handleApplication(scope) {
398398
}
399399
```
400400

401-
Use `scope.once()` rather than `scope.on()`the scope is closed once per worker lifetime, and `once` avoids leaving a listener behind if the plugin registers during a reload.
401+
Use `scope.once()` rather than `scope.on()`closing is a one-time lifecycle event, so `once()` expresses that intent.
402402

403403
Notes on the shutdown sequence:
404404

0 commit comments

Comments
 (0)