workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

🐛 BUG: Importing `getPlatformProxy` hijacks certain `process.on` events

Open rognstadragnar opened this issue 1 year ago • 4 comments

Which Cloudflare product(s) does this pertain to?

Wrangler

What version(s) of the tool(s) are you using?

3.80.2 [Wrangler]

What version of Node are you using?

22.9.0

What operating system and version are you using?

Mac Sequoia 15.0.1

Describe the Bug

Observed behavior

Simply importing getPlatformProxy from wrangler automatically adds process.on('uncaughtException', ...) and process.on('unhandledRejection', ...) listeners that re-throw . This prevents user code from handling those errors.

Expected behavior

Being able to listen to and handle these errors despite using getPlatformProxy

Explanation

Importing runtime APIs like getPlatformProxy leads to importing the code of the cli and some of that code runs on import.

From what I can tell the listeners that hijack these events stem from the yoga-layout package used within ink. The dependency is precompiled and zipped and lives inside the vendor/ directory in this repo.

The specific handlers added (wish I could point directly to the source here but it being precompiled and zipped makes that not possible):

// wrangler/wrangler-dist/cli.js:29768
process.on("uncaughtException", function(a) {
  throw a;
});
process.on(
  "unhandledRejection",
  function(a) {
    throw a;
  }
);

Re-throwing here makes all listeners added after not trigger.

Steps to reproduce

Small repro also provided below, but simply given this code:

import { getPlatformProxy } from 'wrangler';

// This line is not needed, the import alone is enough.
getPlatformProxy().then(proxy => console.log(proxy));

process.on('uncaughtException', error => {
  console.error('Oops! An uncaughtException occurred:', error);
})

process.on('unhandledRejection', error => {
  console.error('Oops! An unhandledRejection occurred:', error);
})

throw Error('This is an error');

One expects to see the error handler called. This is not the case.

<dir>/node_modules/wrangler/wrangler-dist/cli.js:29768
            throw a;
            ^

Error: This is an error
    at file:///<dir>/wrangler-repro/index.js:14:7
    at ModuleJob.run (node:internal/modules/esm/module_job:262:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:483:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:117:5)

Please provide a link to a minimal reproduction

https://github.com/rognstadragnar/cloudflare-wrangler-bug-repro

Please provide any relevant error logs

No response

rognstadragnar avatar Oct 09 '24 13:10 rognstadragnar