Skip to content

fix: ensure Worker subclasses are wrapped only once#126

Merged
ryanking13 merged 6 commits into
mainfrom
gyeongjae/subclasses
Jun 18, 2026
Merged

fix: ensure Worker subclasses are wrapped only once#126
ryanking13 merged 6 commits into
mainfrom
gyeongjae/subclasses

Conversation

@ryanking13

@ryanking13 ryanking13 commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

fixes #125

Our _wrap_subclass had an issue that when there is a subclass that inherits the wrapped WorkerEntrypoint/DurableObject, the _wrap_subclass is called multiple times, double wrapping the env or ctx attributes.

We always had that issue but it was surfaced by my recent change in the DO (#112)

This fixes it by adding a guarder variable and applying the wrapper function only once.

@ask-bonk

ask-bonk Bot commented Jun 16, 2026

Copy link
Copy Markdown

The test structure matches the existing patterns. The fix is clean, the approach is correct, and the tests are thorough. The implementation correctly handles all the edge cases:

  1. Leaf class with no __init__ (inherits wrapped init)
  2. Leaf class with its own __init__ (calls super)
  3. Both DurableObject and WorkerEntrypoint hierarchies are tested
  4. The test asserts that env is wrapped exactly once (not zero, not double)

LGTM

github run

@dom96 dom96 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! See comment for small refactoring nit.

Comment thread packages/runtime-sdk/src/workers/_workers.py Outdated
Comment thread packages/runtime-sdk/src/workers/_workers.py Outdated
Comment thread packages/runtime-sdk/src/workers/_workers.py Outdated
@ryanking13 ryanking13 requested a review from hoodmane June 17, 2026 04:39
@ryanking13 ryanking13 merged commit af8ec42 into main Jun 18, 2026
12 checks passed
@ryanking13 ryanking13 deleted the gyeongjae/subclasses branch June 18, 2026 06:03
hoodmane added a commit to hoodmane/workers-py that referenced this pull request Jun 30, 2026
We fixed the double-wrapping problem in cloudflare#126 but added a new problem that ctx
and env args in subclasses won't be wrapped at all. This switches to wrapping
all __init__ functions but making DurableObjectContext and _EnvWrapper
idempotent so that double wrapping them is not possible.
hoodmane added a commit to hoodmane/workers-py that referenced this pull request Jun 30, 2026
We fixed the double-wrapping problem in cloudflare#126 but added a new problem that ctx
and env args in subclasses won't be wrapped at all. This switches to wrapping
all __init__ functions but making DurableObjectContext and _EnvWrapper
idempotent so that double wrapping them is not possible.
hoodmane added a commit to hoodmane/workers-py that referenced this pull request Jul 1, 2026
We fixed the double-wrapping problem in cloudflare#126 but added a new problem that ctx
and env args in subclasses won't be wrapped at all. This switches to wrapping
all __init__ functions but making DurableObjectContext and _EnvWrapper
idempotent so that double wrapping them is not possible.
hoodmane added a commit that referenced this pull request Jul 1, 2026
)

We fixed the double-wrapping problem in #126 but added a new problem that ctx
and env args in subclasses won't be wrapped at all. This switches to wrapping
all __init__ functions but making DurableObjectContext and _EnvWrapper
idempotent so that double wrapping them is not possible.
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.

Unable to Access ctx.storage During DurableObject Initialization

3 participants