httpx icon indicating copy to clipboard operation
httpx copied to clipboard

Detect blocking calls in coroutines during tests using BlockBuster

Open cbornet opened this issue 9 months ago • 0 comments
trafficstars

Summary

BlockBuster is a library to detect harmful blocking calls done in the async event loop (when activated, it raises an error if a blocking call is done). It is just a few lines to integrate with pytest. Globally, httpx is a good citizen and the only blocking call it detected is mimetypes.guess_type in Request constructor. mimetypes has a global cache so this blocking call is only done once. Solutions for this would be:

  • defer the mimetype resolution to when the request is actually called and call mimetypes.guess_type in a thread
  • or just accept that it blocks for a short time only once and add an exemption in blockbuster setup. I've also seen blocking calls made when using HTTPS but there seems to be no unit tests for async+TLS.

Checklist

  • [x] I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • [x] I've updated the documentation accordingly.

cbornet avatar Jan 30 '25 12:01 cbornet