Skip to content

fix: class-endpoint objects belong to their creator and are bounded in size (#22) - #29

Merged
thorwhalen merged 2 commits into
masterfrom
fix/22-obj-ownership-size
Oct 13, 2026
Merged

thorwhalen merged 2 commits into
masterfrom
fix/22-obj-ownership-size

Conversation

@thorwhalen

@thorwhalen thorwhalen commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Closes #22. Follows the convention of #18 / #23: a safe default plus a named opt-in.

Ownership

  • New handler option obj_owner: (request) -> owner (e.g. the authenticated user id). Objects live in an OwnedObjStore, keyed per owner:
    • another owner's id answers "No object found", the same as a missing id, so it reveals nothing;
    • a request whose owner is None is refused with an AuthorizationError;
    • obj_quota (100 per owner) evicts only that owner's least recently used objects;
    • max_owners (1000) bounds how many owners hold objects at once.
  • Safe default: a class endpoint without obj_owner is refused when the service is built, with an error naming both options.
  • Named opt-in: obj_owner=shared_objects restores the old behaviour, where an object id is the only access control and all clients share one LRU store. obj_store= still applies in that mode only.
  • The request reaches the hook through a new current_request ContextVar, set around every handler call on both the sync and aiohttp paths. Dispatched functions do not have to take the request.

Size

  • Three byte budgets, each disabled with None:
    • max_obj_size: 1 MB per object;
    • max_owner_bytes: 20 MB across one owner's objects;
    • max_total_bytes: 256 MB across the whole endpoint.
  • Sizes are measured after an object is created and after every dispatched call, including calls that raise. The object that takes any budget over its limit is dropped and the client gets an InputError. Evicted objects free their bytes.
  • It is measured by obj_sizeof, which defaults to bounded_deep_sizeof. That function walks containers, __dict__ and __slots__, counts each object once, and stops as soon as the limit is passed, so measuring costs at most about the limit.
  • Measuring stops at max_obj_size, so it costs at most about 1 MB of objects per call. As documented, a single call can still allocate before it is measured, and an object that references large shared data counts that data: raise max_obj_size, or pass an obj_sizeof that skips it.
  • OwnedObjStore is thread-safe.
  • An owner of None or "" is refused. The docs say the owner must come from authentication.

Breaking

Class-endpoint handlers must now set obj_owner. No package in the fleet uses class endpoints. The existing tests were migrated by adding obj_owner=shared_objects in their helper. The decision is recorded for the user in #28, with the recommendation to keep it.

Tests

py2http/tests/test_obj_ownership.py covers:

  • the build-time refusal;
  • that another owner's object cannot be reached;
  • that an unidentified request is refused;
  • that the quota evicts only the owner's own objects;
  • the shared_objects opt-in;
  • dropping an object that grows past the limit;
  • disabling or replacing sizeof;
  • slots;
  • an end-to-end WSGI test in which the owner hook reads the real request header.

pytest --doctest-modules py2http: 119 passed. Dependents gate (priv test-dependents py2http --transitive): py2http and extrude pass.

Adversarial review

A fresh Opus reviewer read the diff and the issue and tried to break it. Across everything it tried, ownership held: one owner never reached another owner's object. Verdict: ship-after-fixes. It confirmed 6 defects. The fixes are in the second commit, and the reviewer's repro scripts now fail as intended:

  1. A method that grows the object and then raises skipped the size check. It reached 50 MB past a 10 KB limit. Fixed: the check now runs in a finally.
  2. There was no overall byte budget. The defaults allowed roughly 1 TB. Fixed: max_owner_bytes and max_total_bytes are added, and the per-object default drops to 1 MB.
  3. A size walk took 129 ms per call on an object near the limit. Fixed by bounding it: the walk stops at max_obj_size, now 1 MB, so it costs about a tenth of that.
  4. An eviction race raised KeyError (665 times in 8 threads). Fixed: the store takes a lock, and a test stresses it.
  5. Objects that reference large shared data are rejected. Documented, together with the two ways out (max_obj_size, obj_sizeof). This is the price of a default that is safe.
  6. The sizeof under-counts closures and memoryviews. Only the class's own code can put data there, not a client. Documented: measurement is approximate.

Plausible concerns also taken:

  • an owner of "" is refused;
  • an unhashable owner gets a clear TypeError;
  • the docs warn that the owner must be authenticated, because client-minted owners can evict other owners.

Not taken, because they are pre-existing or unreachable:

🤖 Generated with Claude Code

thorwhalen and others added 2 commits October 13, 2026 04:46
…n size (#22)

- obj_owner handler hook: (request) -> owner. Objects are kept per owner
  (OwnedObjStore): another owner's id is simply not found, an unidentified
  request is refused, obj_quota (100) evicts only the owner's own least
  recently used objects, max_owners (1000) bounds the owners.
- Safe default: a class endpoint without obj_owner is refused at build time;
  obj_owner=shared_objects is the named opt-in to the old capability-id
  behaviour (obj_store still applies there).
- max_obj_size (10 MB, None disables) measured by obj_sizeof (default
  bounded_deep_sizeof, which stops at the limit) after creation and after
  every dispatched call; an object over it is dropped.
- current_request ContextVar, set around every handler call, so per-request
  hooks can read the request.

Closes #22

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rial review of #29)

- The size check runs in a finally: a call that grows the object and then
  raises is measured too (it grew 50 MB past a 10 KB limit).
- Byte budgets: max_owner_bytes (20 MB) and max_total_bytes (256 MB) on top
  of max_obj_size (now 1 MB, which also bounds the per-call walk). Evicted
  objects release their bytes (LRUObjStore on_evict).
- OwnedObjStore takes a lock (an eviction race raised KeyError under threads).
- obj_owner returning '' is refused like None; an unhashable owner names the
  hook; docs say the owner must be authenticated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thorwhalen
thorwhalen merged commit bd37ee7 into master Oct 13, 2026
6 checks passed
@thorwhalen
thorwhalen deleted the fix/22-obj-ownership-size branch October 13, 2026 04:56
thorwhalen added a commit that referenced this pull request Oct 13, 2026
An instance endpoint (an object exposed through attr_names) is one object every
client shares. Like class endpoints (#29), it now needs obj_owner: a
(request) -> owner hook that refuses unidentified requests, or the named
opt-in obj_owner=shared_objects. Module endpoints are unchanged.

max_obj_size / obj_sizeof bound it: checked when the service is built and
after every call; once over, the object is kept and calls are refused until
it is back under.

AuthorizationError now maps to 401 (its subclasses ForbiddenError and
OverwritesNotAllowed to 403) in the Bottle and aiohttp error handlers, which
share one status table. The aiohttp handler resets current_request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
thorwhalen added a commit that referenced this pull request Oct 13, 2026
* fix: instance endpoints need an obj_owner and are bounded in size (#30)

An instance endpoint (an object exposed through attr_names) is one object every
client shares. Like class endpoints (#29), it now needs obj_owner: a
(request) -> owner hook that refuses unidentified requests, or the named
opt-in obj_owner=shared_objects. Module endpoints are unchanged.

max_obj_size / obj_sizeof bound it: checked when the service is built and
after every call; once over, the object is kept and calls are refused until
it is back under.

AuthorizationError now maps to 401 (its subclasses ForbiddenError and
OverwritesNotAllowed to 403) in the Bottle and aiohttp error handlers, which
share one status table. The aiohttp handler resets current_request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: review of #31: obj_shrinkers, sizeof follows bound methods/partials/closures

- obj_shrinkers: names still callable while an instance endpoint is over its
  limit, so a client can bring it back under (one client could otherwise lock
  everyone out).
- bounded_deep_sizeof follows what bound methods, partials and closures hold.
- Document what is not measured and the per-call cost; fix the 401/403 comment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: second review of #31: checked obj_shrinkers, atomic measure+flag, guarded traversal

- obj_shrinkers: a str is refused, every name must be dispatched.
- Measuring and setting/clearing the over-limit flag happen under one lock.
- bounded_deep_sizeof follows MethodType.__func__, function attributes and
  defaults, static/class methods; an object whose attributes raise is measured
  plainly instead of failing the call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

Class-endpoint objects: no ownership, and the store bounds count not size

1 participant