Skip to content

on_change is skipped when the reload refetch fails #18

Todo

Description

@sujal-maheshwari2004

Summary

When the reload policy accepts a change, the subscription handler drops the cache entry and calls self.get(uri) before calling on_change. If that refetch fails (server briefly unavailable, auth error) the exception escapes the handler and on_change is never called. The same happens when a callable reload policy raises.

Impact

The app silently misses the change notification: its own reload logic never runs, and since the event is consumed nothing retries. Push is documented as best-effort, but losing the callback because of a transient refetch error undermines the reload= feature in exactly the situations (server restarts, deploys) where changes are likely.

Steps to reproduce

  1. pip install "priompt-sdk[nats]"
  2. Run this script. It replaces nats with a minimal fake so the handler can be invoked directly, and points gRPC at a port with no server so the refetch fails:
    import sys, types, asyncio, json
    cbs = []
    fake = types.ModuleType("nats")
    class NC:
        async def subscribe(self, subject, cb=None):
            cbs.append(cb)
    async def connect(url, error_cb=None):
        return NC()
    fake.connect = connect
    sys.modules["nats"] = fake
    
    from priompt_sdk import PromptClient
    c = PromptClient(host="127.0.0.1:1", cache_ttl=30, nats_url="nats://127.0.0.1:4222")
    calls = []
    c.subscribe("priompt://acme/x", lambda v, k: calls.append((v, k)), reload={"minor edit"})
    msg = types.SimpleNamespace(
        data=json.dumps({"version": "abc", "classification": "minor edit"}).encode())
    try:
        asyncio.run(cbs[0](msg))
    except Exception as e:
        print("handler raised:", type(e).__name__, e.code())
    print("on_change calls:", calls)

Actual:

handler raised: _InactiveRpcError StatusCode.UNAVAILABLE
on_change calls: []

With a real broker, nats-py passes the exception to the error callback, which logs a warning; on_change is still not called.

Expected behavior

on_change(version, classification) is called for every event. A failing reload policy or refetch is logged (with the URI) and does not prevent the callback.

Where to look

Suggested approach

  • Wrap the reload policy call and the refetch in try/except Exception, log a warning, and always continue to on_change.
  • Run the blocking refetch off the event loop (await asyncio.get_running_loop().run_in_executor(None, self.get, uri)) so a slow refetch does not stall later events.

Acceptance criteria

  • If the refetch raises, on_change is still called once with the event's version and classification, and a warning is logged.
  • If a callable reload raises, on_change is still called and no refetch is attempted.
  • Tests cover both cases using a faked nats module (as above) and a failing stub.

Activity

  1. added
    bug
    help wanted
    on Nov 14, 2026
  2. maoji-y commented on Dec 2, 2026

    @maoji-y

    Could you assign this to me? I'd like to keep on_change notifications working when the reload policy or refetch fails, with offline regression tests. I'm using Codex and will wait for assignment before opening a PR.

  3. sujal-maheshwari2004 commented on Dec 2, 2026

    @sujal-maheshwari2004
    MemberAuthor

    Assigned to @maoji-y

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bughelp wanted

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions