Skip to content

Conversation

0xbad0c0d3
Copy link
Contributor

@0xbad0c0d3 0xbad0c0d3 commented Sep 5, 2025

fixes #17514
P.S.: It would be better to avoid "side effects" of instrumentation process. But it will require more changes and deeply tested.

@0xbad0c0d3 0xbad0c0d3 changed the title DRAFT: Introduce lock instrumentation for context.waitUntil Introduce lock instrumentation for context.waitUntil to prevent multiple instrumentation Sep 5, 2025
cod1k added 7 commits September 8, 2025 12:16
Added `kFlushLock` to implement and manage a reusable `FlushLock` for the execution context. Enhanced promise handling with `Promise.withResolvers` and introduced type declarations for better developer experience.
Corrected the variable name from `prmise` to `promise` in the mock `waitUntil` function. Added a `props` property to `mockExecutionContext` for enhanced test completeness.
Refactored `getInstrumentedLock` and `storeInstrumentedLock` to use type-safe access via `Lockable` type and direct property manipulation. Removed unnecessary `Object.defineProperty` usage for better readability and maintainability.
Replaced custom delayed `waitUntil` implementations with `Promise.withResolvers` for improved readability and consistency. Removed unnecessary timer usage and adjusted test logic for better maintainability.
Introduced a new test case to validate that `makeFlushLock` does not wrap the execution context more than once. Ensures consistency and prevents potential redundancy in the flush handling logic.
Introduced the `cloneExecutionContext` utility to create shallow copies of the execution context while maintaining proper binding of key methods like `waitUntil` and `passThroughOnException`. Updated all handler proxies to use the cloned execution context, ensuring consistent behavior and preventing unintended side effects.
Introduced a new utility function `createPromiseResolver` to handle promise creation alongside external resolvers. Updated all references of `Promise.withResolvers` in the codebase and tests to use the new utility, ensuring compatibility and maintaining consistency.
cursor[bot]

This comment was marked as outdated.

@s1gr1d s1gr1d requested a review from AbhiPrasad September 8, 2025 15:22
Renamed `cloneExecutionContext` to `copyExecutionContext` and updated its implementation for improved binding logic and symbol handling. Moved `makeFlushLock` to `utils/flushLock` and adjusted imports accordingly. Added tests to validate `copyExecutionContext` behaviors and ensure robustness.
Improved the `copyExecutionContext` utility to prevent re-binding of already bound methods using a proxy. Updated test cases to validate this behavior and added support for `DurableObjectState` as a compatible context type.
@s1gr1d s1gr1d changed the title Introduce lock instrumentation for context.waitUntil to prevent multiple instrumentation feat(cloudflare): Introduce lock instrumentation for context.waitUntil to prevent multiple instrumentation Sep 15, 2025
Copy link
Member

@s1gr1d s1gr1d left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking this over and fixing it! I added a few minor comments

Replaced `copyBound` with `copyAndBindMethod` to simplify and clarify the binding of methods like `waitUntil` and `passThroughOnException`. Improved the implementation to ensure proper proxy handling while retaining context. Added detailed JSDoc comments to enhance maintainability and understanding.
@s1gr1d s1gr1d merged commit 8927921 into getsentry:develop Sep 16, 2025
173 of 175 checks passed
Lms24 added a commit that referenced this pull request Sep 16, 2025
….waitUntil` to prevent multiple instrumentation (#17539)"

This reverts commit 8927921.
andreiborza pushed a commit that referenced this pull request Sep 16, 2025
….waitUntil` to prevent multiple instrumentation (#17539)" (#17666)

This PR reverts #17539 because it causes e2e test fails on PRs as well
as on `develop` and release branches.

It seems like when our e2e tests expect errors in websocket, another
unrelated error is thrown:

```
  ✘  5 [chromium] › tests/index.test.ts:41:5 › Websocket.webSocketMessage (13ms)

[WebServer] ✘ [ERROR] Uncaught TypeError: Illegal invocation: function called with incorrect `this` reference. See https://developers.cloudflare.com/workers/observability/errors/#illegal-invocation-errors for details. Error
[WebServer] 
[WebServer]       at fetch (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/src/index.ts:32:18)
[WebServer]       at null.<anonymous> (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/node_modules/.pnpm/@sentry[email protected]_@[email protected]/node_modules/@sentry/cloudflare/src/durableobject.ts:219:23)
[WebServer]       at null.<anonymous> (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/node_modules/.pnpm/@sentry[email protected]_@[email protected]/node_modules/@sentry/cloudflare/src/request.ts:108:33)
[WebServer]       at handleCallbackErrors.status.status (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/node_modules/.pnpm/@sentry[email protected]/node_modules/@sentry/core/src/tracing/trace.ts:77:15)
[WebServer]       at handleCallbackErrors (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/node_modules/.pnpm/@sentry[email protected]/node_modules/@sentry/core/src/utils/handleCallbackErrors.ts:20:26)
[WebServer]       at null.<anonymous> (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/node_modules/.pnpm/@sentry[email protected]/node_modules/@sentry/core/src/tracing/trace.ts:76:14)
[WebServer]       at null.<anonymous> (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/node_modules/.pnpm/@sentry[email protected]/node_modules/@sentry/core/src/tracing/trace.ts:530:100)
[WebServer]       at null.<anonymous> (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/node_modules/.pnpm/@sentry[email protected]/node_modules/@sentry/core/src/tracing/trace.ts:60:12)
[WebServer]       at null.<anonymous> (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/node_modules/.pnpm/@sentry[email protected]_@[email protected]/node_modules/@sentry/cloudflare/src/async.ts:41:14)
[WebServer]       at withScope (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/node_modules/.pnpm/@sentry[email protected]_@[email protected]/node_modules/@sentry/cloudflare/src/async.ts:40:25)
[WebServer]       at withScope (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/node_modules/.pnpm/@sentry[email protected]/node_modules/@sentry/core/src/currentScopes.ts:59:18)
[WebServer]       at startSpan (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/node_modules/.pnpm/@sentry[email protected]/node_modules/@sentry/core/src/tracing/trace.ts:56:10)
[WebServer]       at null.<anonymous> (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/node_modules/.pnpm/@sentry[email protected]_@[email protected]/node_modules/@sentry/cloudflare/src/request.ts:101:16)
[WebServer]       at null.<anonymous> (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/node_modules/.pnpm/@sentry[email protected]/node_modules/@sentry/core/src/tracing/trace.ts:229:12)
[WebServer]       at null.<anonymous> (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/node_modules/.pnpm/@sentry[email protected]_@[email protected]/node_modules/@sentry/cloudflare/src/async.ts:41:14)
[WebServer]       at withScope (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/node_modules/.pnpm/@sentry[email protected]_@[email protected]/node_modules/@sentry/cloudflare/src/async.ts:40:25)
[WebServer]       at withScope (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/node_modules/.pnpm/@sentry[email protected]/node_modules/@sentry/core/src/currentScopes.ts:65:14)
[WebServer]       at continueTrace (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/node_modules/.pnpm/@sentry[email protected]/node_modules/@sentry/core/src/tracing/trace.ts:226:10)
[WebServer]       at null.<anonymous> (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/node_modules/.pnpm/@sentry[email protected]_@[email protected]/node_modules/@sentry/cloudflare/src/request.ts:95:12)
[WebServer]       at null.<anonymous> (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/node_modules/.pnpm/@sentry[email protected]_@[email protected]/node_modules/@sentry/cloudflare/src/async.ts:56:14)
[WebServer]       at withIsolationScope (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/node_modules/.pnpm/@sentry[email protected]_@[email protected]/node_modules/@sentry/cloudflare/src/async.ts:55:25)
[WebServer]       at withIsolationScope (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/node_modules/.pnpm/@sentry[email protected]/node_modules/@sentry/core/src/currentScopes.ts:114:14)
[WebServer]       at wrapRequestHandler (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/node_modules/.pnpm/@sentry[email protected]_@[email protected]/node_modules/@sentry/cloudflare/src/request.ts:43:10)
[WebServer]       at apply (file:///private/var/folders/63/gxb4ks2n7wbf_7bbwz86cgsc0000gn/T/sentry-e2e-tests-cloudflare-workers-fU9D9e/node_modules/.pnpm/@sentry[email protected]_@[email protected]/node_modules/@sentry/cloudflare/src/durableobject.ts:218:20)
[WebServer] 
[WebServer] 
```

The SDK catches this error instead but the tests fail because they
expect the other error. Not sure what in #17539 caused this but for now
let's unblock ourselves first and follow up with a fix in a new attempt
(cc @0xbad0c0d3 )
@s1gr1d
Copy link
Member

s1gr1d commented Sep 16, 2025

Hey @0xbad0c0d3,
I merged the PR as the CI was green (you can see it above my approval). However, the CI did not include the cloudflare-workers E2E test and this failed before releasing: https://github.com/getsentry/sentry-javascript/actions/runs/17760574492/job/50476183804?pr=17660#step:16:85

Can you take a look at the failing tests? For now, we needed to revert the changes.

@0xbad0c0d3
Copy link
Contributor Author

yes I see, I am looking on it. NP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@sentry/cloudflare crashes DurableObjects due to unsafe & unnecessary proxying of waitUntil()
2 participants