RStudio freezes when calling rstudioapi::getActiveDocumentContext() from patched get_completions()

Hello,

I'm trying to add custom auto-completion functionality to the standard mechanism, by patching the .rs.rpc.get_completions() function using the patch package.

Just for some background, the patch package can be used to hack the code of a function by adding requested custom code at some point in the function body. This question is not about the patch package, and my basic assumption is that the patched code is valid. Assuming that, the question is about the 'new' code itself.

The patched code works, in principle, but in certain situations RStudio freezes. This happens when I'm calling rstudioapi::getActiveDocumentContext()from inside the get_completions() function.

(The standard code of get_completions() doesn't call the rstudioapi::getActiveDocumentContext(), but I'm calling it for getting the context, specifically the code of the buffer or console, which I need for my custom auto-completion mechanism.)

To be specific, I can auto-complete using my patched code by pressing the TAB key. However, if I'm invoking the mechanism at a high rate, say I simply keep the TAB key pressed, a freeze occurs in a short time.

This doesn't happen with the standard auto-completion mechanism - I can leave the TAB key pressed and RStudio won't freeze.

I'm watching the network events that are generated by RStudio client/server communication (right-click in the console area, selecting "inspect element", then select "networking" tab), and it seems that, with the patched code, at some point the HTTP calls do not return and remain in 'pending' status.

The get_completion() function is using RPC, as far as I understand, for calling the server that is actually running the R session, and then - I imagine - the getActiveDocument() function is calling the client for getting the buffer/console context.

So my guess is that invoking this loop at a high rate is causing some kind of a deadlock. However, I'm not sure if this is because I'm using these functions in a way they are not intended to be used, or whether this is another kind of problem.

Also, I couldn't find a different way for getting the buffer/console context, but if there is a different way which is better for using it from inside the get_completions() function I'd be glad to know about it.

Any idea or insight about this issue?

Thanks,
Amit