Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 66 additions & 1 deletion packages/generate/src/load-module.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
Expand Down Expand Up @@ -124,6 +124,71 @@ describe('surviving the module', () => {
expect(Date.now() - started).toBeLessThan(20_000)
}, 40_000)

it('answers at once when a valid module leaves a timer running', async () => {
const pidFile = join(scratch, 'timer.pid')
const path = moduleWith(
'timer.mjs',
`import { writeFileSync } from 'node:fs'\n` +
`writeFileSync(${JSON.stringify(pidFile)}, String(process.pid))\n` +
`setInterval(() => {}, 1000)\n` +
`export const S = { '~standard': { version: 1, vendor: 'x', jsonSchema: { output: () => ({ type: 'string' }) } } }\n`,
)

const started = Date.now()
const result = await loadModuleSchema({
resolvedPath: path,
exportName: 'S',
side: 'output',
})

expect(result).toMatchObject({ ok: true })
if (!result.ok) return
expect(result.document['type']).toBe('string')
// The verdict was ready as soon as the module imported. Anything near the
// 10s budget means the loader waited for the child to exit on its own.
expect(Date.now() - started).toBeLessThan(2_000)

// The timer would keep the child alive for ever, so it must have been
// killed, not left behind.
const pid = Number(readFileSync(pidFile, 'utf8'))
expect(() => process.kill(pid, 0)).toThrow(/ESRCH/)
}, 30_000)

it('is not fooled by a schema whose examples contain {"ok":', async () => {
const path = moduleWith(
'nested-ok.mjs',
`export const S = { '~standard': { version: 1, vendor: 'x', jsonSchema: { output: () => ({ type: 'object', examples: [{ ok: false, code: 'crashed', error: 'not the verdict' }] }) } } }\n`,
)

const result = await loadModuleSchema({
resolvedPath: path,
exportName: 'S',
side: 'output',
})

expect(result).toMatchObject({ ok: true })
if (!result.ok) return
expect(result.document['examples']).toEqual([
{ ok: false, code: 'crashed', error: 'not the verdict' },
])
}, 30_000)

it('is not fooled by a module that prints something shaped like a verdict', async () => {
const path = moduleWith(
'fake-verdict.mjs',
`console.log(JSON.stringify({ ok: false, code: 'crashed', error: 'printed by the module' }))\n` +
`export const S = { '~standard': { version: 1, vendor: 'x', jsonSchema: { output: () => ({ type: 'string' }) } } }\n`,
)

const result = await loadModuleSchema({
resolvedPath: path,
exportName: 'S',
side: 'output',
})

expect(result).toMatchObject({ ok: true })
}, 30_000)

it('reads the verdict even when the module printed on its way in', async () => {
const path = moduleWith(
'chatty.mjs',
Expand Down
76 changes: 55 additions & 21 deletions packages/generate/src/load-module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { spawn } from 'node:child_process'
import { randomUUID } from 'node:crypto'
import { DIALECT_2020_12 } from '@laqi/schema'

/** How long a module gets to import itself and convert. Then the child dies. */
Expand Down Expand Up @@ -39,8 +40,12 @@ const CHILD = `
const { pathToFileURL } = require('node:url')
const request = JSON.parse(process.env.LAQI_MODULE_REQUEST)

// The verdict is one line: a newline, the frame this load was given, then
// JSON (which never holds a raw newline). Exiting in the write callback is
// what keeps a module with a live timer, pool or watcher from holding the
// child open after the answer is already out.
function say(result) {
process.stdout.write(JSON.stringify(result))
process.stdout.write('\\n' + request.frame + JSON.stringify(result) + '\\n', () => process.exit(0))
}

import(pathToFileURL(request.resolvedPath).href).then((module) => {
Expand Down Expand Up @@ -109,14 +114,19 @@ import(pathToFileURL(request.resolvedPath).href).then((module) => {
*/
export async function loadModuleSchema(request: ModuleLoadRequest): Promise<ModuleLoadResult> {
return new Promise((resolve) => {
// Per load, so a module cannot print something that passes for the
// verdict without having read this process's environment.
const frame = `@laqi-verdict:${randomUUID()}:`
const child = spawn(process.execPath, ['-e', CHILD], {
env: { ...process.env, LAQI_MODULE_REQUEST: JSON.stringify(request) },
env: { ...process.env, LAQI_MODULE_REQUEST: JSON.stringify({ ...request, frame }) },
stdio: ['ignore', 'pipe', 'pipe'],
})

let out = ''
let err = ''
let settled = false
let exited = false
let verdict: ModuleLoadResult | undefined

const finish = (result: ModuleLoadResult) => {
if (settled) return
Expand All @@ -125,17 +135,31 @@ export async function loadModuleSchema(request: ModuleLoadRequest): Promise<Modu
resolve(result)
}

// The answer is known, so the child has nothing left to do. It is killed
// here rather than trusted to exit, and the caller hears back only once
// it has exited, so no process outlives the promise.
const accept = (result: ModuleLoadResult) => {
verdict = result.ok ? withDialect(result) : result
child.kill('SIGKILL')
if (exited) finish(verdict)
}

const timer = setTimeout(() => {
child.kill('SIGKILL')
finish({
ok: false,
code: 'timeout',
error: `loading ${request.exportName} took longer than ${MODULE_LOAD_TIMEOUT_MS / 1000} seconds, so laqi stopped it`,
})
finish(
verdict ?? {
ok: false,
code: 'timeout',
error: `loading ${request.exportName} took longer than ${MODULE_LOAD_TIMEOUT_MS / 1000} seconds, so laqi stopped it`,
},
)
}, MODULE_LOAD_TIMEOUT_MS)

child.stdout.on('data', (chunk: Buffer) => {
out += chunk.toString('utf8')
if (verdict !== undefined) return
const found = readResult(out, frame)
if (found !== undefined) accept(found)
})
child.stderr.on('data', (chunk: Buffer) => {
err += chunk.toString('utf8')
Expand All @@ -145,9 +169,14 @@ export async function loadModuleSchema(request: ModuleLoadRequest): Promise<Modu
finish({ ok: false, code: 'crashed', error: cause.message })
})

child.on('exit', () => {
exited = true
if (verdict !== undefined) finish(verdict)
})

child.on('close', (code) => {
if (settled) return
const parsed = readResult(out)
if (settled || verdict !== undefined) return
const parsed = readResult(out, frame)
if (parsed !== undefined) {
finish(parsed.ok ? withDialect(parsed) : parsed)
return
Expand All @@ -166,21 +195,26 @@ export async function loadModuleSchema(request: ModuleLoadRequest): Promise<Modu
}

/**
* The child's verdict, or undefined when stdout holds no verdict at all.
* The child's verdict, or undefined when stdout holds no complete verdict yet.
*
* A loaded module may print whatever it likes, so the JSON is looked for at
* the END of the stream rather than assumed to be all of it. A module that
* logs on import is ordinary, and having that turn into "the loader broke"
* would be a bug report about the wrong thing.
* A loaded module may print whatever it likes, so the verdict is the one line
* that starts with this load's frame, not "the last thing that looks like
* JSON". A module that logs on import is ordinary, and a schema whose
* examples hold `{"ok":` is ordinary too; neither may turn into "the loader
* broke". Only whole lines count, so a verdict still arriving is waited for.
*/
function readResult(out: string): ModuleLoadResult | undefined {
const start = out.lastIndexOf('{"ok":')
if (start === -1) return undefined
try {
return JSON.parse(out.slice(start)) as ModuleLoadResult
} catch {
return undefined
function readResult(out: string, frame: string): ModuleLoadResult | undefined {
const lines = out.split('\n')
// The last piece has no newline after it yet, so it may be half a line.
for (const line of lines.slice(0, -1)) {
if (!line.startsWith(frame)) continue
try {
return JSON.parse(line.slice(frame.length)) as ModuleLoadResult
} catch {
return undefined
}
}
return undefined
}

/**
Expand Down
Loading