Skip to content

Commit

Permalink
Merge pull request #130 from authrequest/main
Browse files Browse the repository at this point in the history
Fix potential memory leak and handle potential erorr creating tmpdir
  • Loading branch information
MichaelTaylor3D authored Oct 26, 2024
2 parents 835c568 + 9b1d039 commit 91336b9
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/controllers/merkleTreeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ const ownerCache = new NodeCache({ stdTTL: ownerCacheTTL });
* @returns {string} sessionId - A unique session identifier.
*/
function createSessionWithTTL(): string {
const tmpDirInfo = tmp.dirSync({ unsafeCleanup: true });
try {
const tmpDirInfo = tmp.dirSync({ unsafeCleanup: true });
} catch (error) {
console.error(`Error creating temporary directory: ${error}`);
}
const sessionId = uuidv4();

const resetTtl = () => {
Expand Down Expand Up @@ -103,13 +107,24 @@ const withIntervalCallback = <T>(
function cleanupSession(sessionId: string): void {
const session = sessionCache[sessionId];
if (session) {
session.cleanup(); // Remove the temporary directory
try {
fs.rmSync(session.tmpDir, { recursive: true, force: true });
} catch (error) {
console.error(`Error removing temporary directory: ${error}`);
// Try to remove the directory again after a 1-second delay
setTimeout(() => {
try {
fs.rmSync(session.tmpDir, { recursive: true, force: true });
} catch (error) {
console.error(`Error removing temporary directory (retry): ${error}`);
}
}, 1000);
}
clearTimeout(session.timer); // Clear the timeout
delete sessionCache[sessionId]; // Remove the session from the cache
console.log(`Session ${sessionId} cleaned up.`);
}
}

async function merkleIntegrityCheck(
treePath: string,
tmpDir: string,
Expand Down

0 comments on commit 91336b9

Please sign in to comment.