Skip to content

feat: stop watching for updates with an AbortSignal - #1002

Merged
runk merged 1 commit into
runk:masterfrom
hassanabrar202:feat/stop-watching
Nov 13, 2026
Merged

runk merged 1 commit into
runk:masterfrom
hassanabrar202:feat/stop-watching

Conversation

@hassanabrar202

Copy link
Copy Markdown
Contributor

Follow-up to #1000.

Problem

open() with watchForUpdates: true registers an fs.watchFile listener, and nothing can remove it:

  • with the default persistent watcher, a process that has finished its work never exits, so graceful shutdown needs process.exit() or watchForUpdatesNonPersistent;
  • opening the database again, for example after a config change or in tests, adds another listener, and the old reader keeps being reloaded in the background.

Change

A new signal option, the standard AbortSignal pattern:

const controller = new AbortController();
const lookup = await maxmind.open(path, { watchForUpdates: true, signal: controller.signal });

// later, e.g. on shutdown
controller.abort();
  • Aborting calls fs.unwatchFile with the same listener.
  • A change that was already being handled when the signal fired doesn't reload the reader.
  • An already-aborted signal doesn't start a watcher.
  • The reader keeps serving the database it has loaded.

The option is additive, so existing calls behave as before. The README documents it.

Tests

  • Two new tests. One checks that aborting calls unwatchFile(path, listener) and that a queued change is then ignored. The other checks that an already-aborted signal starts no watcher. Both fail on master.
  • npm test: 59 passed. tsc and prettier are clean.
  • I also checked with a real process and no stubs. With a persistent watcher, open() followed by abort() lets the process exit right away. Without the signal, it keeps running.

open() with watchForUpdates registers an fs.watchFile listener that
nothing can remove. A persistent watcher then keeps the process alive at
shutdown, and opening the database again adds another listener that
keeps reloading the old reader.

Add a `signal` option. Aborting it removes the listener and ignores a
change that was already being handled; the reader keeps serving the
database it has loaded. An already-aborted signal does not start a
watcher.
@runk
runk merged commit 8e1443c into runk:master Nov 13, 2026
4 checks passed
@runk

runk commented Nov 13, 2026

Copy link
Copy Markdown
Owner

Thanks for the addition!

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.

2 participants