Skip to content

Bring nats.micro in line with the service ADR - #1131

Draft
caspervonb wants to merge 2 commits into
mainfrom
fix-micro-conformance
Draft

caspervonb wants to merge 2 commits into
mainfrom
fix-micro-conformance

Conversation

@caspervonb

Copy link
Copy Markdown
Collaborator

Running nats.micro through the ADR-32 conformance suite showed it disagreeing with the service ADR in several places: metadata kept by reference, reset() moving the start time instead of clearing counters, respond_error not counted in stats, a null INFO description, endpoints allowed under $SRV, invalid queue groups accepted, empty error codes and descriptions accepted, and stop() dropping requests still being handled.

Refusing queue groups that hold whitespace, ., * or >, endpoint subjects under $SRV, and empty error codes or descriptions is a behavior change for callers that relied on them.

Running the legacy service API through the ADR-32 conformance suite
turned up a set of places where it disagrees with the specification:

- Metadata was kept by reference, so changing the dict after creating
  a service or endpoint changed what INFO reports.
- reset() moved the start time and left the endpoint counters alone.
- An error sent with respond_error was not counted in the stats.
- INFO sent a null description when none was set.
- Endpoints could subscribe to subjects under $SRV.
- Queue groups holding whitespace, '.', '*' or '>' were accepted.
- respond_error accepted an empty code or description.
- stop() dropped requests that were still being handled.

Refusing these queue groups, subjects and error values is a change in
behavior for callers that relied on them.
@caspervonb caspervonb added the nats-legacy Legacy asyncio NATS client label Nov 23, 2026
@synadia-claude-reviewer

synadia-claude-reviewer Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Claude finished @caspervonb's task in 2m 24s —— View job


Reviewing PR #1131

  • Gather context (PR diff, changed files)
  • Review nats/src/nats/micro/request.py
  • Review nats/src/nats/micro/service.py
  • Review nats/tests/test_micro_service.py
  • Post inline comments for any real issues
  • Post final summary

Summary

Reviewed the ADR-32 conformance fixes (metadata copying, reset(), error counting, stop()→drain(), reserved-subject/queue-group validation, empty-description/code rejection). Traced through Subscription._drain() to confirm stop() really does wait for in-flight requests now, and confirmed the _on_error callback dedup logic in Request/Endpoint._handle_request matches the new tests.

Found one real issue: in service.py:276, if a handler raises a ServiceError with an empty code/description, the new validation in respond_error (request.py:78-82) raises ValueError from inside the except block uncaught — no response is sent and the caller's RPC times out instead of getting an error reply. Left as an inline comment.

Also noted (non-blocking): Service.stop() drains endpoints sequentially, so stop time is the sum rather than the max of each endpoint's in-flight processing.

Full summary posted as a PR comment.

Comment thread nats/src/nats/micro/service.py Outdated
self._record_error(repr(error))

if isinstance(error, ServiceError):
await request.respond_error(error.code, error.description)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

respond_error now raises ValueError for an empty code or description (request.py:78-82). If a handler raises a ServiceError with an empty code/description, this line will raise that ValueError from inside the except block, which is not caught here. It propagates out of _handle_request into the subscription's message loop, where it's only passed to error_cb (nats/src/nats/aio/subscription.py:315-319) — no response is ever sent to the caller, so the original RPC will time out instead of receiving an error response (a behavior regression vs. before this validation existed). Worth guarding against, e.g. falling back to a default code/description here instead of forwarding a possibly-invalid ServiceError.

@synadia-claude-reviewer

Copy link
Copy Markdown

Review summary

Verified the fixes against the current _handle_request/stop/drain flow — the error-counting dedup via Request._on_error, the stop()→drain() switch (confirmed Subscription._drain() does pending_queue.join() before unsubscribing, so in-flight requests really do complete), the metadata copy-on-read/write, and reset() now resetting per-endpoint counters all check out and match their new tests.

One correctness concern, left as an inline comment on service.py:276: respond_error now raises ValueError for an empty code/description (request.py:78-82). If a handler raises a ServiceError with an empty field, that ValueError escapes the except block in _handle_request uncaught, so no response is ever sent and the caller's RPC times out instead of getting an error reply — a silent regression for that edge case.

Minor/non-blocking: Service.stop() still drains endpoints sequentially (for endpoint in self._endpoints: await endpoint._stop()), so total stop time is the sum of each endpoint's in-flight processing rather than the max. Not wrong, just worth knowing if a service has many endpoints with slow in-flight handlers.

View job run

respond_error now refuses an empty code or description, so a handler
raising ServiceError with either left empty made the framework's own
error response raise, and the caller timed out without a reply. Fall
back to code 500 and the error's repr instead.

This branch has not been deployed

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

Labels

nats-legacy Legacy asyncio NATS client

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant