Skip to content

Handle async servlet dispatch and streaming responses in Logbook filter#2249

Merged
kasmarian merged 2 commits intozalando:mainfrom
aukevanleeuwen:discussions/2248-missing-async-response-body
Mar 4, 2026
Merged

Handle async servlet dispatch and streaming responses in Logbook filter#2249
kasmarian merged 2 commits intozalando:mainfrom
aukevanleeuwen:discussions/2248-missing-async-response-body

Conversation

@aukevanleeuwen
Copy link
Contributor

I've opened a discussion here: #2248 with some more details surrounding this PR.

@aukevanleeuwen
Copy link
Contributor Author

As commented here: #2248 (comment) I was expecting this build to fail on coverage, but I need some input to see if we can even get rid of this branch altogether?

WDYT?

@aukevanleeuwen aukevanleeuwen force-pushed the discussions/2248-missing-async-response-body branch from f270c12 to 34a66a7 Compare February 18, 2026 14:07
@aukevanleeuwen
Copy link
Contributor Author

I've added a very convoluted test to imitate a duplicate call to LogbookFilter#write(...). The branch coverage is now at 100%. Marking this PR as ready for review.

@aukevanleeuwen
Copy link
Contributor Author

@kasmarian (just picking one of the maintainers here): there is some code in RemoteRequest to attach the listener when startAsync is called. I've done it in a simpler way in the MR by just adding the listener to the async context after the return from the chain.filter() call.

Can we just get rid of that code in favor of https://github.com/aukevanleeuwen/logbook/blob/34a66a72c34df2b335f4419d378f5bcaf0148c0b/logbook-servlet/src/main/java/org/zalando/logbook/servlet/LogbookFilter.java#L76?

@kasmarian kasmarian added the bugfix Bug fixes and patches label Feb 19, 2026
@kasmarian
Copy link
Member

A very interesting find, @aukevanleeuwen! I tried finding a way to break it without the AtomicBoolean, but seems like the flow works fine with your changes, as it should have from the beginning, is seems. As AtomicBoolean was just patching a bug. Feel free to remove it in this PR.

The concern I had was that in case of these multi-step flows, LogbookFilter would be called several times with different AsyncContexts, each of which would trigger completion events and result in multiple duplicated log writes. But it looks like eventually only one onComplete.

The flow with the internally forwarded request from the test that you added looked like:

  1. (first request)
    asyncStarted = false
    dispatcherType = REQUEST
    -> doFilter
    asyncStarted = true
    -> LogbookFilter adds the listener

  2. (second internal request)
    asyncStarted = false
    dispatcherType = ASYNC
    -> doFilter
    asyncStarted = true
    dispatcherType = FORWARD
    somewhere here asyncContext.startAsync is called which resets listeners.
    -> LogbookFilter adds the listener again, but to a new context

asyncStarted = false
dispatcherType = ASYNC
....
onComplete() -> LogbookFilter.write()

@aukevanleeuwen aukevanleeuwen force-pushed the discussions/2248-missing-async-response-body branch from 34a66a7 to f2f88ec Compare February 20, 2026 10:08
@aukevanleeuwen
Copy link
Contributor Author

@kasmarian I've rebased and removed the synchronizer code (and the async listener code in RemoteRequest). Let me know if you have any comments.

@aukevanleeuwen
Copy link
Contributor Author

@lukasniemeier-zalando @kasmarian @msdousti @ChristianLohmann Any chance this can have another review? TIA.

@kasmarian
Copy link
Member

sorry, somehow half of the logbook maintainers got sick last week. The changes look good to me

@kasmarian
Copy link
Member

👍

@aukevanleeuwen
Copy link
Contributor Author

@lukasniemeier-zalando @msdousti @ChristianLohmann Can one of you do another review perhaps?

@lukasniemeier-zalando
Copy link
Member

👍

@kasmarian kasmarian merged commit 4f85e4e into zalando:main Mar 4, 2026
4 checks passed
@aukevanleeuwen aukevanleeuwen deleted the discussions/2248-missing-async-response-body branch March 6, 2026 04:34
@aukevanleeuwen
Copy link
Contributor Author

@kasmarian : do you have a release cadence of some kind by any chance?

@kasmarian
Copy link
Member

we don't have a fixed cadence. I was waiting for a few other PRs to get resolved and reviewed by my colleagues, e.g. #2234 and #2184 to batch them together.
Do you want your fix soon? We can do a small release sooner for you, not a problem

@aukevanleeuwen
Copy link
Contributor Author

Hey @kasmarian, that would be nice if possible. I can understand you want to batch them together, but to be honest it doesn't look like they will be done very soon. But maybe I'm mistaken. If they are close to finishing, fine. Otherwise I'd love to have a release earlier.

@kasmarian
Copy link
Member

Let me check with a few people how close they feel their PR reviews are and do a release later this week either with other changes or not

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

Labels

bugfix Bug fixes and patches

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants