fix: better kill process tensorrt-llm (#2681)

This commit is contained in:
Louis 2024-04-11 12:47:41 +07:00 committed by GitHub
parent ebdaaa6c10
commit c0949b2d7e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 57 additions and 27 deletions

View File

@ -37,10 +37,10 @@
"@rollup/plugin-json": "^6.1.0", "@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3", "@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-replace": "^5.0.5", "@rollup/plugin-replace": "^5.0.5",
"@types/decompress": "4.2.7",
"@types/node": "^20.11.4", "@types/node": "^20.11.4",
"@types/os-utils": "^0.0.4", "@types/os-utils": "^0.0.4",
"@types/tcp-port-used": "^1.0.4", "@types/tcp-port-used": "^1.0.4",
"@types/decompress": "4.2.7",
"cpx": "^1.5.0", "cpx": "^1.5.0",
"download-cli": "^1.1.1", "download-cli": "^1.1.1",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
@ -58,6 +58,7 @@
"path-browserify": "^1.0.1", "path-browserify": "^1.0.1",
"rxjs": "^7.8.1", "rxjs": "^7.8.1",
"tcp-port-used": "^1.0.2", "tcp-port-used": "^1.0.2",
"terminate": "^2.6.1",
"ulidx": "^2.3.0" "ulidx": "^2.3.0"
}, },
"engines": { "engines": {
@ -72,6 +73,7 @@
"tcp-port-used", "tcp-port-used",
"fetch-retry", "fetch-retry",
"decompress", "decompress",
"@janhq/core" "@janhq/core",
"terminate"
] ]
} }

View File

@ -9,12 +9,14 @@ import {
PromptTemplate, PromptTemplate,
} from '@janhq/core/node' } from '@janhq/core/node'
import decompress from 'decompress' import decompress from 'decompress'
import terminate from 'terminate'
// Polyfill fetch with retry // Polyfill fetch with retry
const fetchRetry = fetchRT(fetch) const fetchRetry = fetchRT(fetch)
const supportedPlatform = (): string[] => ['win32', 'linux'] const supportedPlatform = (): string[] => ['win32', 'linux']
const supportedGpuArch = (): string[] => ['ampere', 'ada'] const supportedGpuArch = (): string[] => ['ampere', 'ada']
const PORT_CHECK_INTERVAL = 100
/** /**
* The response object for model init operation. * The response object for model init operation.
@ -64,12 +66,12 @@ async function loadModel(
/** /**
* Stops a Engine subprocess. * Stops a Engine subprocess.
*/ */
function unloadModel(): Promise<any> { function unloadModel(): Promise<void> {
const controller = new AbortController() const controller = new AbortController()
setTimeout(() => controller.abort(), 5000) setTimeout(() => controller.abort(), 5000)
debugLog(`Request to kill engine`) debugLog(`Request to kill engine`)
subprocess?.kill() const killRequest = () => {
return fetch(TERMINATE_ENGINE_URL, { return fetch(TERMINATE_ENGINE_URL, {
method: 'DELETE', method: 'DELETE',
signal: controller.signal, signal: controller.signal,
@ -78,14 +80,43 @@ function unloadModel(): Promise<any> {
subprocess = undefined subprocess = undefined
}) })
.catch(() => {}) // Do nothing with this attempt .catch(() => {}) // Do nothing with this attempt
.then(() => tcpPortUsed.waitUntilFree(parseInt(ENGINE_PORT), 300, 5000)) // Wait for port available .then(() =>
tcpPortUsed.waitUntilFree(
parseInt(ENGINE_PORT),
PORT_CHECK_INTERVAL,
5000
)
) // Wait for port available
.then(() => debugLog(`Engine process is terminated`)) .then(() => debugLog(`Engine process is terminated`))
.catch((err) => { .catch((err) => {
debugLog( debugLog(
`Could not kill running process on port ${ENGINE_PORT}. Might be another process running on the same port? ${err}` `Could not kill running process on port ${ENGINE_PORT}. Might be another process running on the same port? ${err}`
) )
return { err: 'PORT_NOT_AVAILABLE' } throw 'PORT_NOT_AVAILABLE'
}) })
}
if (subprocess?.pid) {
log(`[NITRO]::Debug: Killing PID ${subprocess.pid}`)
const pid = subprocess.pid
return new Promise((resolve, reject) => {
terminate(pid, function (err) {
if (err) {
return killRequest()
} else {
return tcpPortUsed
.waitUntilFree(parseInt(ENGINE_PORT), PORT_CHECK_INTERVAL, 5000)
.then(() => resolve())
.then(() => log(`[NITRO]::Debug: Nitro process is terminated`))
.catch(() => {
killRequest()
})
}
})
})
} else {
return killRequest()
}
} }
/** /**
* 1. Spawn engine process * 1. Spawn engine process
@ -97,11 +128,6 @@ async function runEngineAndLoadModel(
systemInfo: SystemInformation systemInfo: SystemInformation
) { ) {
return unloadModel() return unloadModel()
.then((res) => {
if (res?.error) {
throw new Error(res.error)
}
})
.then(() => runEngine(systemInfo)) .then(() => runEngine(systemInfo))
.then(() => loadModelRequest(settings)) .then(() => loadModelRequest(settings))
.catch((err) => { .catch((err) => {
@ -220,7 +246,9 @@ async function runEngine(systemInfo: SystemInformation): Promise<void> {
reject(`child process exited with code ${code}`) reject(`child process exited with code ${code}`)
}) })
tcpPortUsed.waitUntilUsed(parseInt(ENGINE_PORT), 300, 30000).then(() => { tcpPortUsed
.waitUntilUsed(parseInt(ENGINE_PORT), PORT_CHECK_INTERVAL, 30000)
.then(() => {
debugLog(`Engine is ready`) debugLog(`Engine is ready`)
resolve() resolve()
}) })