Skip to content

Commit

Permalink
fix(bundles ans-104): clean up promise try/catch PE-4306
Browse files Browse the repository at this point in the history
Try/catch in the parent thread wasn't correctly cleaning up the promise.
This would cause the next call to block indefinitely. This change also
removes the 'wait' for promise cleanup since the queue method now waits
on the promise returned from parseBundle.
  • Loading branch information
djwhitt committed Jul 27, 2023
1 parent 1cce224 commit 8c9bb9d
Showing 1 changed file with 2 additions and 11 deletions.
13 changes: 2 additions & 11 deletions src/lib/ans-104.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
parentPort,
workerData,
} from 'node:worker_threads';
import { default as wait } from 'wait';
import * as winston from 'winston';

import * as events from '../events.js';
Expand Down Expand Up @@ -103,7 +102,6 @@ export class Ans104Parser {
private log: winston.Logger;
private worker: Worker;
private contiguousDataSource: ContiguousDataSource;
private unbundlePromise: Promise<void> | undefined;
private unbundlePromiseResolve: (() => void) | undefined;
private unbundlePromiseReject: ((reason?: any) => void) | undefined;

Expand Down Expand Up @@ -165,7 +163,6 @@ export class Ans104Parser {
}

resetUnbundlePromise() {
this.unbundlePromise = undefined;
this.unbundlePromiseResolve = undefined;
this.unbundlePromiseReject = undefined;
}
Expand All @@ -184,14 +181,7 @@ export class Ans104Parser {
try {
this.unbundlePromiseResolve = resolve;
this.unbundlePromiseReject = reject;

const log = this.log.child({ parentId });
log.debug('Waiting for previous bundle to finish...');
while (this.unbundlePromise) {
await wait(100);
}
log.debug('Previous bundle finished.');

await fsPromises.mkdir(path.join(process.cwd(), 'data/tmp/ans-104'), {
recursive: true,
});
Expand All @@ -204,7 +194,7 @@ export class Ans104Parser {
const writeStream = fs.createWriteStream(bundlePath);
pipeline(data.stream, writeStream, (error) => {
if (error !== undefined) {
this.unbundlePromiseReject?.(error);
reject(error);
this.resetUnbundlePromise();
log.error('Error writing ANS-104 bundle stream', error);
} else {
Expand All @@ -219,6 +209,7 @@ export class Ans104Parser {
});
} catch (error) {
reject(error);
this.resetUnbundlePromise();
}
},
);
Expand Down

0 comments on commit 8c9bb9d

Please sign in to comment.