Skip to content

Commit b2941ab

Browse files
committed
added the code necessary to catch the error event from the spawn and send it to the callback
1 parent c24d95f commit b2941ab

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

lib/pem.js

+22-12
Original file line numberDiff line numberDiff line change
@@ -534,28 +534,38 @@ function spawnOpenSSL(params, callback) {
534534
// making this fail periodically.
535535
var needed = 2; // wait for both exit and close.
536536
var code = -1;
537-
var bothDone = function() {
538-
if (code) {
539-
callback(new Error("Invalid openssl exit code: " + code + "\n% openssl " + params.join(" ") + "\n" + stderr), code);
540-
} else {
541-
callback(null, code, stdout, stderr);
537+
var finished = false;
538+
var done = function (err) {
539+
if (finished) {
540+
return;
541+
}
542+
543+
if (err) {
544+
finished = true;
545+
return callback(err);
546+
}
547+
548+
if (--needed < 1) {
549+
finished = true;
550+
if (code) {
551+
callback(new Error("Invalid openssl exit code: " + code + "\n% openssl " + params.join(" ") + "\n" + stderr), code);
552+
} else {
553+
callback(null, code, stdout, stderr);
554+
}
542555
}
543556
};
544557

558+
openssl.on('error', done);
559+
545560
openssl.on('exit', function (ret) {
546561
code = ret;
547-
if (--needed < 1) {
548-
bothDone();
549-
}
562+
done();
550563
});
551564

552565
openssl.on('close', function () {
553566
stdout = new Buffer(stdout, "binary").toString("utf-8");
554567
stderr = new Buffer(stderr, "binary").toString("utf-8");
555-
556-
if (--needed < 1) {
557-
bothDone();
558-
}
568+
done();
559569
});
560570
}
561571

0 commit comments

Comments
 (0)