fix(events): parse newline-delimited JSON from Docker event stream#270
Conversation
|
Warning Review limit reached
Next review available in: 37 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughThe Docker event listener now processes newline-delimited JSON stream payloads, filters container events, and invokes a callback with deserialized events. The event command callback explicitly declares a ChangesDocker event stream handling
Estimated code review effort: 3 (Moderate) | ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/Client/DockerApiClientWrapper.php (1)
74-78: 🚀 Performance & Scalability | 🔵 Trivial | 💤 Low valueRedundant double JSON decode.
$lineis parsed twice: once viajson_decodefor theTypefilter check, and again inside$serializer->deserialize(). Consider denormalizing the already-decoded$eventObjectarray instead of re-parsing the raw string.♻️ Avoid re-parsing the same JSON string
$eventObject = json_decode(json: $line, associative: true, depth: 512, flags: JSON_THROW_ON_ERROR); if (is_array($eventObject) && ($eventObject['Type'] ?? false) === 'container') { - $event = $serializer->deserialize(data: $line, type: ContainerEvent::class, format: 'json'); + $event = $serializer->denormalize(data: $eventObject, type: ContainerEvent::class); $eventCallback($event); }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/Client/DockerApiClientWrapper.php` around lines 74 - 78, Update the event handling around $eventObject and $serializer->deserialize() to denormalize the already-decoded array instead of passing $line for a second JSON parse. Preserve the existing container Type filter and eventCallback invocation.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/Client/DockerApiClientWrapper.php`:
- Around line 54-80: Import the remaining global functions used in the streaming
loop of the Docker API client wrapper: add function imports for strpos, substr,
trim, and json_validate alongside the existing imports, without changing the
logic around stream processing or event deserialization.
---
Nitpick comments:
In `@src/Client/DockerApiClientWrapper.php`:
- Around line 74-78: Update the event handling around $eventObject and
$serializer->deserialize() to denormalize the already-decoded array instead of
passing $line for a second JSON parse. Preserve the existing container Type
filter and eventCallback invocation.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: ace2dbb6-8563-4c6e-b4da-8845c0693c22
📒 Files selected for processing (2)
src/Client/DockerApiClientWrapper.phpsrc/Command/EventsListenCommand.php
992309f to
2100c93
Compare
Docker's /events endpoint returns newline-delimited JSON with Content-Type: application/json, not Server-Sent Events. The old loop only handled ServerSentEvent chunks, which a plain HttpClient never emits, so listenForEvents() never invoked the callback and container events were silently dropped. - Buffer raw data chunks and split on newlines, decoding each complete JSON line and denormalizing the decoded array (no second JSON parse). - Drop the duplicate /events request issued on every loop iteration. - Inject an optional event-stream HttpClient so the method is unit testable via MockHttpClient; production still builds the socket-bound client by default. - Add unit tests for dispatch filtering, chunk buffering and invalid JSON handling. Drive-by: cs:fix adds a missing closure return type in EventsListenCommand.
c2011f9 to
cf60c67
Compare
## [1.4.4](1.4.3...1.4.4) (2026-07-13) ### Bug Fixes * **events:** parse newline-delimited JSON from Docker event stream ([#270](#270)) ([ecd385e](ecd385e))
|
🎉 This PR is included in version 1.4.4 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
Problem
DockerApiClientWrapper::listenForEvents() never invoked its callback — container events were silently dropped.
Root cause: Docker's /events endpoint returns newline-delimited JSON (Content-Type: application/json), not Server-Sent Events. The old loop only processed ServerSentEvent chunks, which a plain Symfony HttpClient never emits (only EventSourceHttpClient does). It also fired a duplicate /events request on every while() iteration.
Empirically confirmed against a live daemon: chunks arrive as FirstChunk + DataChunk, never ServerSentEvent, so the "instanceof ServerSentEvent" branch was dead code.
Fix
Tests
New unit tests for listenForEvents() using MockHttpClient:
Verification
Notes