From b8bd17ddbdb68a018d85bd4ccb67cbb2d37b0bf7 Mon Sep 17 00:00:00 2001 From: Nelson Chan <3271800+chakflying@users.noreply.github.com> Date: Sun, 26 Nov 2023 18:47:56 +0800 Subject: [PATCH] Fix: Add timeout to testDockerHost (#4097) --- server/docker.js | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/server/docker.js b/server/docker.js index a96324a9f..bec0e0b12 100644 --- a/server/docker.js +++ b/server/docker.js @@ -1,10 +1,10 @@ const axios = require("axios"); const { R } = require("redbean-node"); -const version = require("../package.json").version; const https = require("https"); const fs = require("fs"); const path = require("path"); const Database = require("./database"); +const { axiosAbortSignal } = require("./util-server"); class DockerHost { @@ -70,9 +70,11 @@ class DockerHost { static async testDockerHost(dockerHost) { const options = { url: "/containers/json?all=true", + timeout: 5000, headers: { "Accept": "*/*", }, + signal: axiosAbortSignal(6000), }; if (dockerHost.dockerType === "socket") { @@ -82,26 +84,33 @@ class DockerHost { options.httpsAgent = new https.Agent(DockerHost.getHttpsAgentOptions(dockerHost.dockerType, options.baseURL)); } - let res = await axios.request(options); + try { + let res = await axios.request(options); - if (Array.isArray(res.data)) { + if (Array.isArray(res.data)) { - if (res.data.length > 1) { + if (res.data.length > 1) { + + if ("ImageID" in res.data[0]) { + return res.data.length; + } else { + throw new Error("Invalid Docker response, is it Docker really a daemon?"); + } - if ("ImageID" in res.data[0]) { - return res.data.length; } else { - throw new Error("Invalid Docker response, is it Docker really a daemon?"); + return res.data.length; } } else { - return res.data.length; + throw new Error("Invalid Docker response, is it Docker really a daemon?"); + } + } catch (e) { + if (e.code === "ECONNABORTED" || e.name === "CanceledError") { + throw new Error("Connection to Docker daemon timed out."); + } else { + throw e; } - - } else { - throw new Error("Invalid Docker response, is it Docker really a daemon?"); } - } /**