Skip to content

Commit 61af70d

Browse files
Merge pull request #235 from Sebastian-Webster/fix-process-already-killed
Fix process already killed on Windows
2 parents e6c7074 + 2726552 commit 61af70d

File tree

6 files changed

+39
-15
lines changed

6 files changed

+39
-15
lines changed

.github/workflows/os-compatibility.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ jobs:
3333
- name: Run tests
3434
env:
3535
VERSION_REQUIREMENT: ${{ matrix.version-requirement }}
36+
MOVE_MYSQLMSN_TO: ${{ runner.os == 'Windows' && 'C:\\Users\\RUNNER~1\\mysqlmsn' || '/tmp/mysqlmsn' }}
3637
run: npm run os-compat:ci
3738

3839
- name: Upload mysqlmsn directory (Windows)

ciSetup.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ process.env.useCIDBPath = true;
44

55
const GitHubActionsTempFolder = process.platform === 'win32' ? 'C:\\Users\\RUNNER~1\\mysqlmsn' : '/tmp/mysqlmsn'
66

7-
process.env.mysqlmsn_internal_DO_NOT_USE_databaseDirectoryPath = normalize(GitHubActionsTempFolder + '/dbs')
87
process.env.mysqlmsn_internal_DO_NOT_USE_binaryDirectoryPath = normalize(GitHubActionsTempFolder + '/binaries')
98

109
process.env.mysqlmsn_internal_DO_NOT_USE_deleteDBAfterStopped = false;

dist/src/constants.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export declare const LOG_LEVELS: {
88
};
99
declare const internalOptions: {
1010
deleteDBAfterStopped: string;
11-
databaseDirectoryPath: string;
1211
binaryDirectoryPath: string;
1312
cli: string;
1413
};

src/constants.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ export const LOG_LEVELS = {
3232
const internalOptions = {
3333
deleteDBAfterStopped: 'true',
3434
//mysqlmsn = MySQL Memory Server Node.js
35-
databaseDirectoryPath: normalizePath(`${tmpdir()}/mysqlmsn/dbs`),
3635
binaryDirectoryPath: `${tmpdir()}/mysqlmsn/binaries`,
3736
cli: 'false'
3837
}

src/libraries/Executor.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,30 @@ class Executor {
3636
}
3737

3838
async #killProcess(process: ChildProcess): Promise<boolean> {
39-
let killed = false;
39+
// If the process has already been killed, return true
40+
if (process.kill(0) === false) {
41+
this.logger.warn('Called #killProcess to kill mysqld but it has already been killed.')
42+
return true
43+
}
44+
4045
if (os.platform() === 'win32') {
4146
const {error, stderr} = await this.#executeFile('taskkill', ['/pid', String(process.pid), '/t', '/f'])
42-
if (!error && !stderr) {
43-
killed = true;
44-
} else {
45-
this.logger.error(error || stderr)
47+
const message = error || stderr
48+
49+
if (!message) {
50+
return true
51+
}
52+
53+
if (message.toString().includes('There is no running instance of the task')) {
54+
this.logger.warn('Called #killProcess and tried to kill mysqld process but taskkill could not because it is not running. Error received:', message)
55+
return true
4656
}
47-
} else {
48-
killed = process.kill()
57+
58+
this.logger.error(message, '| Error toString:', message.toString())
59+
return false
4960
}
50-
return killed;
61+
62+
return process.kill('SIGKILL')
5163
}
5264

5365
//Returns a path to the binary if it should be deleted
@@ -134,8 +146,9 @@ class Executor {
134146
fs.unwatchFile(errorLogFile)
135147

136148
if (signal) {
137-
this.logger.log('Exiting because of aborted signal.')
138-
return
149+
this.logger.log('Exiting because the process received a signal:', signal)
150+
} else {
151+
this.logger.log('Exiting with code:', code)
139152
}
140153

141154
let errorLog: string;
@@ -169,7 +182,9 @@ class Executor {
169182

170183
try {
171184
if (getInternalEnvVariable('deleteDBAfterStopped') === 'true') {
185+
this.logger.log('Deleting database path as deleteDBAfterStopped is true...')
172186
await fsPromises.rm(dbPath, {recursive: true, force: true, maxRetries: 50, retryDelay: 100})
187+
this.logger.log('Database deletion was successful.')
173188
}
174189
} catch (e) {
175190
this.logger.error('An error occurred while deleting database directory at path:', dbPath, '| The error was:', e)
@@ -527,7 +542,7 @@ class Executor {
527542

528543
let retries = 0;
529544

530-
this.databasePath = normalizePath(`${getInternalEnvVariable('databaseDirectoryPath')}/${randomUUID().replaceAll("-", '')}`)
545+
this.databasePath = normalizePath(`${os.tmpdir()}/mysqlmsn/dbs/${randomUUID().replaceAll("-", '')}`)
531546

532547
const datadir = normalizePath(`${this.databasePath}/data`)
533548

tests/versions.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import {expect, test, jest} from '@jest/globals'
1+
import {expect, test, jest, afterAll} from '@jest/globals'
22
import { createDB } from '../src/index'
33
import sql from 'mysql2/promise'
44
import { coerce, satisfies } from 'semver';
55
import { ServerOptions } from '../types';
66
import getBinaryURL from '../src/libraries/Version';
77
import { DOWNLOADABLE_MYSQL_VERSIONS } from '../src/constants';
8+
import fs from 'fs'
9+
import fsPromises from 'fs/promises'
10+
import os from 'os'
811

912
const usernames = ['root', 'dbuser']
1013

@@ -59,4 +62,12 @@ for (const version of DOWNLOADABLE_MYSQL_VERSIONS.filter(v => satisfies(v, versi
5962
//binary, we need this test here just in case all the MySQL binaries are skipped
6063
test('dummy test', () => {
6164
expect(1 + 1).toBe(2)
65+
})
66+
67+
afterAll(async () => {
68+
const originalPath = `${os.tmpdir()}/mysqlmsn`
69+
if (process.env.MOVE_MYSQLMSN_TO && fs.existsSync(originalPath) && originalPath !== process.env.MOVE_MYSQLMSN_TO) {
70+
await fsPromises.cp(originalPath, process.env.MOVE_MYSQLMSN_TO, {recursive: true, force: true, filter: source => !source.includes('.sock')})
71+
await fsPromises.rm(originalPath, {force: true, recursive: true, maxRetries: 50, retryDelay: 100})
72+
}
6273
})

0 commit comments

Comments
 (0)