Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: await stream close before unlink and keep temp-dir cleanup reachable (#5654)
Adam Eivybd7fb1b0bf91
diff --git a/server/routes/appleHealth.js b/server/routes/appleHealth.jsindex 407a6cd06d..a211bd09d4 100644--- a/server/routes/appleHealth.js+++ b/server/routes/appleHealth.js@@ -209,7 +209,10 @@ router.post('/import/xml', uploadXml, asyncHandler(async (req, res) => {throw err;});- await fs.unlink(filePath);+ // Tolerate a failed unlink of the uploaded ZIP (same posture as the reject+ // branch above): throwing here would skip the finally that removes+ // extractDir and leak the far larger extracted export.xml.+ await fs.unlink(filePath).catch(() => {});filePath = xmlPath;console.log(`📋 Found ${clinicalJsons.length} clinical record files in ZIP`);}diff --git a/server/services/appleHealthXml.js b/server/services/appleHealthXml.jsindex 07cb17f65e..d4d178af31 100644--- a/server/services/appleHealthXml.js+++ b/server/services/appleHealthXml.js@@ -322,6 +322,10 @@ export async function importAppleHealthXml(filePath, io = null) {// Windows unlink fails while the file is open. An Apple Health export.xml// is routinely 0.5-3GB, so a leaked temp file is expensive.inputStream.destroy();+ // destroy() tears the fd down asynchronously, so wait for 'close' before+ // unlinking — on Windows the unlink fails while the handle is open and the+ // failure is swallowed below (same wait the ZIP write-stream cleanup does).+ if (!inputStream.closed) await new Promise((res) => inputStream.once('close', res));await unlink(filePath).catch(() => {});}diff --git a/server/services/appleHealthXml.test.js b/server/services/appleHealthXml.test.jsindex b34bcbc26e..8e3c023631 100644--- a/server/services/appleHealthXml.test.js+++ b/server/services/appleHealthXml.test.js@@ -24,20 +24,34 @@ vi.mock('./appleHealthIngest.js', async (importOriginal) => {};});-// Capture the read streams the service opens so the fd-release half of the-// contract is assertable (unlink alone succeeds on POSIX with the fd still open).+// Capture the read streams the service opens, and trace stream-close vs unlink+// order: unlink alone succeeds on POSIX with the fd still open, so only the+// ordering proves the fd was released first (it is what Windows requires).const streams = [];+const trace = vi.hoisted(() => []);vi.mock('fs', async (importOriginal) => {const actual = await importOriginal();return {...actual,createReadStream: (...args) => {const s = actual.createReadStream(...args);+ s.on('close', () => trace.push('close'));streams.push(s);return s;},};});+vi.mock('fs/promises', async (importOriginal) => {+ const actual = await importOriginal();+ return {+ ...actual,+ default: actual,+ unlink: async (...args) => {+ trace.push('unlink');+ return actual.unlink(...args);+ },+ };+});const { importAppleHealthXml } = await import('./appleHealthXml.js');@@ -55,6 +69,7 @@ describe('importAppleHealthXml temp-file lifecycle', () => {beforeEach(async () => {streams.length = 0;+ trace.length = 0;store.writeDayFile = async () => {};dir = await mkdtemp(join(tmpdir(), 'portos-apple-health-test-'));xmlPath = join(dir, 'export.xml');@@ -72,6 +87,7 @@ describe('importAppleHealthXml temp-file lifecycle', () => {expect(await exists(xmlPath)).toBe(false);expect(streams).toHaveLength(1);expect(streams[0].destroyed).toBe(true);+ expect(trace).toEqual(['close', 'unlink']);});it('removes the input file and releases its fd when the flush rejects', async () => {@@ -84,5 +100,8 @@ describe('importAppleHealthXml temp-file lifecycle', () => {expect(await exists(xmlPath)).toBe(false);expect(streams).toHaveLength(1);expect(streams[0].destroyed).toBe(true);+ // Order matters: destroy() releases the fd asynchronously, so unlinking+ // before 'close' would fail on Windows and silently leak the file.+ expect(trace).toEqual(['close', 'unlink']);});});