Skip to content

Commit

Permalink
Fix disposing the docker daemon request on non-network errors
Browse files Browse the repository at this point in the history
Change-type: patch
  • Loading branch information
thgreasi committed Nov 9, 2023
1 parent db962ac commit 5959fea
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
3 changes: 0 additions & 3 deletions .mocharc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
module.exports = {
timeout: 25000,
spec: 'test/**/*.spec.ts',
// TODO: This shouldn't be necessary, but the tests on node20 hang, while they pass on node 16 & 18.
// `leaked-handles` showed an unclosed docker deamon connection.
exit: true,
};
25 changes: 23 additions & 2 deletions lib/build/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import * as fs from 'mz/fs';
import * as path from 'path';
import { Duplex } from 'stream';
import * as tar from 'tar-stream';
import type * as http from 'http';

// Import hook definitions
import * as Plugin from './plugin';
Expand Down Expand Up @@ -83,6 +84,7 @@ export default class Builder {
const failBuild = _.once((err: Error) => {
streamError = err;
dup.destroy(err);
cleanupDaemonStream?.();
return this.callHook(
hooks,
'buildFailure',
Expand All @@ -96,6 +98,8 @@ export default class Builder {
inputStream.on('error', failBuild);
dup.on('error', failBuild);

let cleanupDaemonStream: (() => void) | undefined;

const buildPromise = (async () => {
const daemonStream = await this.docker.buildImage(inputStream, buildOpts);

Expand All @@ -104,7 +108,25 @@ export default class Builder {
daemonStream,
layers,
fromTags,
reject,
(err) => {
cleanupDaemonStream = () => {
if (
'req' in daemonStream! &&
daemonStream.req != null &&
typeof daemonStream.req === 'object' &&
'destroy' in daemonStream.req &&
typeof daemonStream.req.destroy === 'function'
) {
const req = daemonStream.req as http.ClientRequest;
if (!req.destroyed) {
req.destroy();
}
}
daemonStream.unpipe();
cleanupDaemonStream = undefined;
};
reject(err);
},
);
outputStream.on('error', (error: Error) => {
daemonStream.unpipe();
Expand Down Expand Up @@ -275,7 +297,6 @@ function getDockerDaemonBuildOutputParserStream(
this.emit('data', data.stream);
}
} catch (error) {
daemonStream.unpipe();
onError(error);
}
}),
Expand Down

0 comments on commit 5959fea

Please sign in to comment.