Ensures proper cleanup of converter resources
Addresses potential resource leaks by ensuring that temporary files and event listeners are properly cleaned up when the converter is disposed. This prevents issues with multiple conversions, especially on platforms like Windows where file locking can occur. The `uncaughtException` handler is removed to avoid swallowing errors, and a flag is added to prevent double cleanup.
This commit is contained in:
1 file changed
+35
-9
@@ -74,6 +74,8 @@ export class PandocTypstConverter {
|
|||||||
private plugin: obsidianTypstPDFExport;
|
private plugin: obsidianTypstPDFExport;
|
||||||
private commandBuilder: PandocCommandBuilder;
|
private commandBuilder: PandocCommandBuilder;
|
||||||
private pandocExecutor: PandocExecutor;
|
private pandocExecutor: PandocExecutor;
|
||||||
|
private processCleanupHandler: (() => void) | null = null;
|
||||||
|
private isDisposed = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new PandocTypstConverter instance with configuration.
|
* Creates a new PandocTypstConverter instance with configuration.
|
||||||
@@ -185,10 +187,9 @@ export class PandocTypstConverter {
|
|||||||
// Add the output path to the result if successful
|
// Add the output path to the result if successful
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
result.outputPath = outputPath;
|
result.outputPath = outputPath;
|
||||||
|
progressCallback?.('Conversion complete!', 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
progressCallback?.('Conversion complete!', 100);
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -287,19 +288,26 @@ export class PandocTypstConverter {
|
|||||||
* - Normal exit
|
* - Normal exit
|
||||||
* - SIGINT (Ctrl+C)
|
* - SIGINT (Ctrl+C)
|
||||||
* - SIGTERM (kill command)
|
* - SIGTERM (kill command)
|
||||||
* - Uncaught exceptions
|
*
|
||||||
|
* Note: Handlers are stored and removed on dispose() to prevent listener leaks.
|
||||||
|
* The uncaughtException handler is NOT registered to avoid swallowing errors.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
private setupCleanup(): void {
|
private setupCleanup(): void {
|
||||||
const cleanup = () => {
|
// Store handler reference so we can remove it later
|
||||||
this.cleanup();
|
this.processCleanupHandler = () => {
|
||||||
|
// Only cleanup if not already disposed to avoid double-cleanup
|
||||||
|
if (!this.isDisposed) {
|
||||||
|
this.cleanup();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
process.on('exit', cleanup);
|
// Register cleanup handlers - these will be removed on dispose()
|
||||||
process.on('SIGINT', cleanup);
|
process.on('exit', this.processCleanupHandler);
|
||||||
process.on('SIGTERM', cleanup);
|
process.on('SIGINT', this.processCleanupHandler);
|
||||||
process.on('uncaughtException', cleanup);
|
process.on('SIGTERM', this.processCleanupHandler);
|
||||||
|
// Note: uncaughtException handler removed to avoid swallowing errors
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -380,6 +388,9 @@ export class PandocTypstConverter {
|
|||||||
* calling dispose explicitly allows for immediate cleanup and is recommended
|
* calling dispose explicitly allows for immediate cleanup and is recommended
|
||||||
* when the converter is no longer needed.
|
* when the converter is no longer needed.
|
||||||
*
|
*
|
||||||
|
* This method also removes all registered process event listeners to prevent
|
||||||
|
* memory leaks and allow the converter instance to be garbage collected.
|
||||||
|
*
|
||||||
* @returns Promise that resolves when cleanup is complete
|
* @returns Promise that resolves when cleanup is complete
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
@@ -393,6 +404,21 @@ export class PandocTypstConverter {
|
|||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
async dispose(): Promise<void> {
|
async dispose(): Promise<void> {
|
||||||
|
if (this.isDisposed) {
|
||||||
|
return; // Already disposed, avoid double-cleanup
|
||||||
|
}
|
||||||
|
|
||||||
|
this.isDisposed = true;
|
||||||
|
|
||||||
|
// Remove process event listeners to prevent memory leaks
|
||||||
|
if (this.processCleanupHandler) {
|
||||||
|
process.off('exit', this.processCleanupHandler);
|
||||||
|
process.off('SIGINT', this.processCleanupHandler);
|
||||||
|
process.off('SIGTERM', this.processCleanupHandler);
|
||||||
|
this.processCleanupHandler = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cleanup temporary files and resources
|
||||||
await this.cleanup();
|
await this.cleanup();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user