Skip to content

run handlers after responding instead of from set_timeout_async - #2927

Merged
rchl merged 2 commits into
mainfrom
fix/post-response
May 24, 2026
Merged

rchl merged 2 commits into
mainfrom
fix/post-response

Conversation

@rchl

@rchl rchl commented May 23, 2026

Copy link
Copy Markdown
Member

I thought hard how to avoid set_timeout_async but still run handler after responding and this is the solution.

Request handler can return a tuple(Promise, callback) instead of Promise to run callback right after responding, without posting a new task with set_timeout_async.

Fixes #2926

Comment thread plugin/api.py
def request_handler(
method: str
) -> Callable[[Callable[[Any, P], Promise[R]]], Callable[[Any, P, int], Promise[Response[R]]]]:
) -> Callable[[Callable[[Any, P], RequestHandlerResponse]], Callable[[Any, P, int], Promise[Response[R]]]]:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we really need this for plugins too? This seems to be quite niche problem that only occurs for the "refresh" requests, because we want to send the response for those first before doing further actions. I wouldn't expect the same to be so relevant to custom LSP requests, so I would assume it to be sufficient for plugins to do the work first, before sending the response, or alternatively live with set_timeout / set_timeout_async.

This possibility to return a tuple with additional callback is currently not mentioned in the docstring.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I agree and that's why I have not documented it. I would consider this an internal feature even if it leaks to the public API.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder wouldn't it be better then to allow this only for the internal code, perhaps by using a different decorator variant that is not exposed in the public API?

But maybe it's not so important if we prefer to avoid the code duplication that might be needed for that?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added complexity and maintenance wouldn't be worth it IMO.
It's not documented so it's not clear what it does without reading internal code so that should keep people away from using it.
And if someone uses it, nothing bad will happen anyway.

@rwols

rwols commented May 24, 2026

Copy link
Copy Markdown
Member

If I may be honest here: we are going to have to solve this problem again with the asyncio PR. And the solutions here don’t translate cleanly to asyncio. So… maybe pause this?

@rchl

rchl commented May 24, 2026

Copy link
Copy Markdown
Member Author

If I may be honest here: we are going to have to solve this problem again with the asyncio PR. And the solutions here don’t translate cleanly to asyncio. So… maybe pause this?

No, it should translate just fine, I think this should work: 7276e53

@rchl rchl changed the title run handlers directly after responding instead of set_timeout_async run handlers after responding instead of from set_timeout_async May 24, 2026
@rwols

rwols commented May 24, 2026

Copy link
Copy Markdown
Member

No, it should translate just fine

Alright, I'll trust you then. πŸ‘

@rchl

rchl commented May 24, 2026

Copy link
Copy Markdown
Member Author

I think I've more or less gotten two OKs, even if not explicit, so will merge and update the asyncio task after.

@rchl
rchl merged commit 44f568a into main May 24, 2026
8 checks passed
@rchl
rchl deleted the fix/post-response branch May 24, 2026 21:12
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.

Extra unwanted diagnostics cancelations

3 participants