Skip to content

Commit

Permalink
javascript: fix handler leak (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
extremeheat authored Feb 14, 2024
1 parent fcec267 commit 5a2bd1e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
18 changes: 13 additions & 5 deletions src/javascript/js/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ class Bridge {
const v = await this.m[ffid]
this.ipc.send({ r, val: v.valueOf() })
}

async blob (r, ffid) {
const v = await this.m[ffid]
this.ipc.sendBlob(v, r)
}

async keys (r, ffid) {
const v = await this.m[ffid]
const keys = Object.getOwnPropertyNames(v)
Expand Down Expand Up @@ -258,7 +258,7 @@ const ipc = {
process.stderr.write(JSON.stringify(data) + '\n')
},
sendBlob: (data, r) => {
process.stderr.write('blob!{"r":'+r+',"len":'+data.length+'}!')
process.stderr.write('blob!{"r":' + r + ',"len":' + data.length + '}!')
process.stderr.write(data)
process.stderr.write('\n')
},
Expand All @@ -283,7 +283,11 @@ process.stdin.on('data', data => {
for (const line of message.split('\n')) {
try { var j = JSON.parse(line) } catch (e) { continue } // eslint-disable-line
if (j.c === 'pyi') {
handlers[j.r]?.(j)
const handler = handlers[j.r]
if (handler) {
handler(j)
delete handlers[j.r]
}
} else {
bridge.onMessage(j)
}
Expand All @@ -302,7 +306,11 @@ process.stdin.on('end', () => {
for (const line of message.split('\n')) {
try { var j = JSON.parse(line) } catch (e) { continue } // eslint-disable-line
if (j.c === 'pyi') {
handlers[j.r]?.(j)
const handler = handlers[j.r]
if (handler) {
handler(j)
delete handlers[j.r]
}
} else {
bridge.onMessage(j)
}
Expand Down
4 changes: 1 addition & 3 deletions src/javascript/js/pyi.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,17 @@ class PyBridge {
) {
const ffid = ++this.jsi.ffid
this.jsi.m[ffid] = v
this.queueForCollection(ffid, v)
return { ffid }
}
}
return v
})

const stacktrace = new PythonException(stack)

const resp = await waitFor(resolve => this.com.writeRaw(payload, r, resolve), timeout || REQ_TIMEOUT, () => {
throw new BridgeException(`Attempt to access '${stack.join('.')}' failed.`)
})
if (resp.key === 'error') {
const stacktrace = new PythonException(stack)
stacktrace.setPythonTrace(resp.sig)
throw stacktrace
}
Expand Down
4 changes: 2 additions & 2 deletions src/pythonia/Bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class Bridge {
const ffid = pre.val[r]
// Python is the owner of the memory, we borrow a ref to it and once
// we're done with it (GC'd), we can ask python to free it
if (made[r] instanceof Promise) throw Error('You did not await a paramater when calling ' + stack.join('.'))
if (made[r] instanceof Promise) throw Error('You did not await a parameter when calling ' + stack.join('.'))
this.jsi.m[ffid] = made[r]
this.queueForCollection(ffid, made[r])
}
Expand Down Expand Up @@ -296,7 +296,7 @@ class Bridge {
}

/**
* This method creates a Python class which proxies overriden entries on the
* This method creates a Python class which proxies overridden entries on the
* on the JS side over to JS. Conversely, in JS when a property access
* is performed on an object that doesn't exist, it's sent to Python.
*/
Expand Down

0 comments on commit 5a2bd1e

Please sign in to comment.