From b90ae0835aec5484279eddcea4e9ccaa253d2db0 Mon Sep 17 00:00:00 2001 From: Michael FIG Date: Mon, 24 May 2021 22:28:33 -0600 Subject: [PATCH] fix: don't create new promise IDs and stall the pipeline --- packages/captp/lib/captp.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/captp/lib/captp.js b/packages/captp/lib/captp.js index f74aab94b0c..d1efc05cc58 100644 --- a/packages/captp/lib/captp.js +++ b/packages/captp/lib/captp.js @@ -188,6 +188,21 @@ export function makeCapTP(ourId, rawSend, bootstrapObj = undefined, opts = {}) { // eslint-disable-next-line no-use-before-define const pr = makeRemoteKit(questionID); questions.set(questionID, pr); + + // To fix #2846: + // We return 'p' to the handler, and the eventual resolution of 'p' will + // be used to resolve the caller's Promise, but the caller never sees 'p' + // itself. The caller got back their Promise before the handler ever got + // invoked, and thus before queueMessage was called. If that caller + // passes the Promise they received as argument or return value, we want + // it to serialize as resultVPID. And if someone passes resultVPID to + // them, we want the user-level code to get back that Promise, not 'p'. + lastPromiseID += 1; + const promiseID = lastPromiseID; + const resultVPID = `p+${promiseID}`; + valToSlot.set(pr.p, resultVPID); + slotToVal.set(resultVPID, pr.p); + return [questionID, pr]; }