Skip to content

fix: close daemon socket after programmatic load() to prevent process hang#717

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-daemon-socket-open-issue
Draft

fix: close daemon socket after programmatic load() to prevent process hang#717
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-daemon-socket-open-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 25, 2026

After await load() resolves keychain(...) values, the process hangs because the daemon client's Unix socket (~/.varlock/local-encrypt/daemon.sock) remains open and ref'd, keeping the event loop alive indefinitely.

Changes

  • daemon-client.ts — Call socket.unref() on connect. The idle socket no longer holds the event loop open. In-flight sendMessage calls are unaffected: their setTimeout timers keep the event loop alive while waiting for responses (including biometric/interactive ops with their 5-min timeout).

  • local-encrypt/index.ts — Add cleanupDaemonClient() helper that calls cleanup() on the singleton if one was created. Belt-and-suspenders for runtimes where unref() alone may not suffice.

  • index.ts — Call cleanupDaemonClient() at the end of load(), and export a public cleanup() for users who need explicit lifecycle control:

import { load, cleanup } from 'varlock';

await load(); // daemon socket is now closed automatically

// or explicitly, e.g. after direct keychain usage outside load():
cleanup();

- Add socket.unref() in DaemonClient.connectToSocket so the idle socket
  does not keep the event loop alive after all messages have resolved.
- Add cleanupDaemonClient() helper in local-encrypt/index.ts.
- Call cleanupDaemonClient() at the end of the programmatic load() function
  so the daemon socket is explicitly closed after env resolution completes.
- Export a public cleanup() function from varlock/src/index.ts for
  programmatic users who want explicit lifecycle control.
Copilot AI changed the title [WIP] Fix daemon socket remaining open after keychain resolution fix: close daemon socket after programmatic load() to prevent process hang May 25, 2026
Copilot AI requested a review from philmillman May 25, 2026 02:03
@philmillman
Copy link
Copy Markdown
Member

@theoephraim might be unnecessary (see linked issue) feel free to close

@philmillman philmillman requested a review from theoephraim May 25, 2026 14:02
@theoephraim
Copy link
Copy Markdown
Member

seems reasonable, but currently we dont even talk about using load() anywhere, or provide any other guidance. Im wondering if we should get rid of it, or at least mark it as internal or something 🤷

@philmillman
Copy link
Copy Markdown
Member

seems reasonable, but currently we dont even talk about using load() anywhere, or provide any other guidance. Im wondering if we should get rid of it, or at least mark it as internal or something 🤷

yeah I'll defer to you there. I think we can close

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.

[BUG]: Programmatic load() can leave daemon socket open after resolving keychain(...)

3 participants