Last active
December 7, 2021 00:35
-
-
Save bartlomieju/38ab143e0b460f2f69f06d42525e12b1 to your computer and use it in GitHub Desktop.
Demonstrate event loop hang if "Runtime.runIfWaitingForDebugger" is not called after connecting to Deno inspector. Forked from https://gist.github.com/evanwashere/04cfa233f613343e1c9d6fb72cfd628c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Emitter { | |
#on = Object.create(null); | |
#once = Object.create(null); | |
on(n, fn) { (this.#on[n] ?? (this.#on[n] = [])).push(fn); } | |
once(n, fn) { (this.#once[n] ?? (this.#once[n] = [])).push(fn); } | |
emit(n, ...args) { this.#on[n]?.forEach(_ => _(...args)); if (this.#once[n]) this.#once[n] = (this.#once[n].forEach(_ => _(...args)), undefined); } | |
off(n, fn) { if (!fn) (delete this.#on[n], delete this.#once[n]); else { let o = this.#on[n]?.indexOf(fn) ?? -1; if (o !== -1) this.#on[n].splice(o, 1); o = this.#once[n]?.indexOf(fn) ?? -1; if (o !== -1) this.#once[n].splice(o, 1); } } | |
} | |
class Inspector extends Emitter { | |
#seq = 0; | |
#socket = null; | |
#promises = Object.create(null); | |
async call(method, params) { | |
if (!this.#socket) throw new Error('not connected'); | |
return await new Promise((ok, err) => { | |
this.#promises[this.#seq] = { ok, err }; | |
this.#socket.send(JSON.stringify({ method, params, id: this.#seq++ })); | |
}); | |
} | |
async connect(url) { | |
return await new Promise((ok, err) => { | |
this.#socket = new WebSocket(url); | |
this.#socket.onopen = () => ok(); | |
this.#socket.onerror = _ => err(_.error); | |
this.#socket.onclose = () => { | |
this.#seq = 0; | |
this.#socket = null; | |
const e = new Error('connection closed'); | |
for (const _ in this.#promises) this.#promises[_].err(e); | |
this.#promises = Object.create(null); | |
}; | |
this.#socket.onmessage = _ => { | |
const { id, error, method, params, result } = JSON.parse(_.data); | |
if (method === 'Runtime.executionContextDestroyed') this.#socket.close(); | |
else if (method) this.emit(method, params); | |
else { | |
if (!error) this.#promises[id].ok(result); | |
else { | |
const e = new Error(error.message); | |
this.#promises[id].err((e.code = error.code, e)); | |
} | |
delete this.#promises[id]; | |
} | |
}; | |
}); | |
} | |
} | |
const inspectorUrl = Deno.args[0]; | |
if (!inspectorUrl.startsWith("ws://")) { | |
throw new Error("Invalid inspector URL"); | |
} | |
const inspector = new Inspector; | |
await inspector.connect(inspectorUrl); | |
const context = new Promise(r => inspector.once('Runtime.executionContextCreated', _ => r(_.context))); | |
console.log('connected'); | |
await inspector.call('Runtime.enable'); | |
// Remove this comment to unhang event loop | |
// await inspector.call('Runtime.runIfWaitingForDebugger'); | |
async function evl(expr) { | |
const { result } = await inspector.call('Runtime.evaluate', { | |
replMode: true, | |
expression: expr, | |
awaitPromise: true, | |
objectGroup: 'repl', | |
generatePreview: true, | |
contextId: (await context).id, | |
}); | |
await inspector.call('Runtime.releaseObjectGroup', { objectGroup: 'repl' }); | |
return result; | |
} | |
while (true) { | |
console.log(await evl(prompt())); | |
} | |
await inspector.call('Runtime.disable'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To run: