-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(cloudflare): Introduce lock instrumentation for context.waitUntil
to prevent multiple instrumentation
#17539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
context.waitUntil
context.waitUntil
to prevent multiple instrumentation
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.
d329c82
to
4d46261
Compare
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.
d00a8c2
to
2249f68
Compare
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.
context.waitUntil
to prevent multiple instrumentationcontext.waitUntil
to prevent multiple instrumentation
There was a problem hiding this 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.
….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 )
Hey @0xbad0c0d3, Can you take a look at the failing tests? For now, we needed to revert the changes. |
yes I see, I am looking on it. NP |
fixes #17514
P.S.: It would be better to avoid "side effects" of instrumentation process. But it will require more changes and deeply tested.