Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(docker): close the connection of a call that timed out
Yudhi Armyndharis04557d5510cd
diff --git a/src/modules/docker/docker-unresponsive.spec.ts b/src/modules/docker/docker-unresponsive.spec.tsindex e27e51553..d4afe06f4 100644--- a/src/modules/docker/docker-unresponsive.spec.ts+++ b/src/modules/docker/docker-unresponsive.spec.ts@@ -40,4 +40,20 @@ describe('DockerService against a daemon that never answers', () => {expect(Date.now() - started).toBeLessThan(2000);expect(service.isDockerAvailable()).toBe(false);}, 10_000);++ // A call that is only raced against its deadline keeps its request and its socket, and every later+ // availability check starts another one, until the process runs out of descriptors.+ it('closes the connection of every call it gave up on', async () => {+ const service = new DockerService();+ service.callTimeoutMs = 100;+ await service.onModuleInit();++ for (let i = 0; i < 6; i++) {+ service.isDockerAvailable();+ await new Promise(resolve => setTimeout(resolve, 150));+ }+ await new Promise(resolve => setTimeout(resolve, 200));++ expect(sockets.size).toBeLessThanOrEqual(1);+ }, 15_000);});diff --git a/src/modules/docker/docker.service.spec.ts b/src/modules/docker/docker.service.spec.tsindex f93c9a6ff..82ade3130 100644--- a/src/modules/docker/docker.service.spec.ts+++ b/src/modules/docker/docker.service.spec.ts@@ -277,6 +277,7 @@ describe('prestartBuiltinDatabase', () => {expect(docker.listContainers).toHaveBeenCalledWith({all: true,filters: { label: ['com.openwa.service=database'] },+ abortSignal: expect.any(AbortSignal),});expect(container.start).toHaveBeenCalledTimes(1);});@@ -413,6 +414,7 @@ describe('DockerService.getContainerByService label match and guard rails', () =expect(listContainers).toHaveBeenCalledWith({all: true,filters: { label: ['com.openwa.service=database'] },+ abortSignal: expect.any(AbortSignal),});expect(getContainer).toHaveBeenCalledWith('label-hit-1');expect(result).toEqual({ id: 'label-hit-1' });diff --git a/src/modules/docker/docker.service.ts b/src/modules/docker/docker.service.tsindex 53f71b511..6eb63e1a0 100644--- a/src/modules/docker/docker.service.ts+++ b/src/modules/docker/docker.service.ts@@ -43,22 +43,37 @@ export class DockerService implements OnModuleInit {* dockerode sets no timeout, so a daemon or socket proxy that accepts the connection and never* answers (a dropped SYN, a wedged upstream) would hold the caller, boot included, until the OS or* the proxy gave up. Short calls are bounded here; image pulls and container starts are not.+ *+ * The call is aborted when the deadline passes, not only raced: a call left running keeps its request+ * and its socket open, and every later availability check would add one more.*/- private async answered<T>(call: Promise<T>, what: string): Promise<T> {+ private async answered<T>(call: (signal: AbortSignal) => Promise<T>, what: string): Promise<T> {+ const controller = new AbortController();let timer: NodeJS.Timeout | undefined;const deadline = new Promise<never>((_, reject) => {- timer = setTimeout(- () => reject(new Error(`Docker did not answer ${what} within ${this.callTimeoutMs}ms`)),- this.callTimeoutMs,- );+ timer = setTimeout(() => {+ reject(new Error(`Docker did not answer ${what} within ${this.callTimeoutMs}ms`));+ controller.abort();+ }, this.callTimeoutMs);});try {- return await Promise.race([call, deadline]);+ return await Promise.race([call(controller.signal), deadline]);} finally {clearTimeout(timer);}}+ /**+ * dockerode's runtime takes an options object with `abortSignal` on every call, which its typings+ * declare only for some of them.+ */+ private abortable<T>(method: 'ping' | 'info', abortSignal: AbortSignal): Promise<T> {+ const call = this.docker![method].bind(this.docker) as unknown as (options: {+ abortSignal: AbortSignal;+ }) => Promise<T>;+ return call({ abortSignal });+ }+async onModuleInit() {await this.initializeDocker();// Bootstrap orchestration: start containers based on saved config@@ -115,7 +130,7 @@ export class DockerService implements OnModuleInit {private async initializeDocker(): Promise<void> {try {this.docker = new Docker(this.buildDockerOptions());- await this.answered(this.docker.ping(), 'a ping');+ await this.answered(abortSignal => this.abortable<unknown>('ping', abortSignal), 'a ping');this.isAvailable = true;this.logger.log('Docker API connected successfully');} catch (error) {@@ -167,7 +182,10 @@ export class DockerService implements OnModuleInit {}try {- const containers = await this.answered(this.docker.listContainers({ all: true }), 'a container list');+ const containers = await this.answered(+ abortSignal => this.docker!.listContainers({ all: true, abortSignal }),+ 'a container list',+ );return containers.filter(c => {// Filter by OpenWA labels or name prefix@@ -212,12 +230,14 @@ export class DockerService implements OnModuleInit {try {const containers = await this.answered(- this.docker.listContainers({- all: true,- filters: {- label: [`com.openwa.service=${service}`],- },- }),+ abortSignal =>+ this.docker!.listContainers({+ all: true,+ filters: {+ label: [`com.openwa.service=${service}`],+ },+ abortSignal,+ }),'a container list',);@@ -228,7 +248,10 @@ export class DockerService implements OnModuleInit {// Fallback: try by EXACT name (never a substring — a substring, and especially the empty// string, would resolve an arbitrary container). OpenWA-managed containers are `openwa-<service>`.const target = `openwa-${service}`;- const allContainers = await this.answered(this.docker.listContainers({ all: true }), 'a container list');+ const allContainers = await this.answered(+ abortSignal => this.docker!.listContainers({ all: true, abortSignal }),+ 'a container list',+ );const match = allContainers.find(c => c.Names?.some(n => n === target || n === `/${target}`));if (match) {@@ -652,7 +675,10 @@ export class DockerService implements OnModuleInit {}try {- const info = (await this.answered(this.docker.info(), 'the daemon info')) as {+ const info = (await this.answered(+ abortSignal => this.abortable<unknown>('info', abortSignal),+ 'the daemon info',+ )) as {Containers: number;ContainersRunning: number;ContainersPaused: number;