2121 await_request ,
2222 next_request ,
2323)
24- from wit_world .imports .http_req import send
2524from wit_world .imports .http_resp import send_downstream
2625from wit_world .imports .types import Err , Error_CannotRead
2726
@@ -201,39 +200,17 @@ def __call__(self):
201200
202201 def handle (self , request : Any , body : Any ) -> None :
203202 """Handle incoming HTTP requests by serving them through the WSGI app."""
204- serve_wsgi_request (
205- request ,
206- body ,
207- self .wsgi_app ,
208- handle_errors = self .handle_errors ,
209- )
203+ with request : # Ensure dropping of request resource before trying to get another one. This dodges a crash.
204+ serve_wsgi_request (
205+ request ,
206+ body ,
207+ self .wsgi_app ,
208+ handle_errors = self .handle_errors ,
209+ )
210210
211211 if not self .reuse_sandboxes_for_ms :
212212 return
213213
214- try :
215- # Drop (in the WIT sense) the `request` resource to get ready for
216- # another request. Otherwise, we crash.
217- #
218- # Here we abuse an arbitrary request-consuming function to trigger
219- # the drop. Glue code interposed by wasmtime's linker ensures that
220- # drop happens, but send() otherwise fails before doing anything.
221- send (request , body , "no such backend" )
222-
223- # TODO: Generate a proper drop_whatever() function for each
224- # "whatever" resource.
225- #
226- # Alternately, it might suffice for the runtime to drop() things
227- # that get GC'd (i.e. `del` or otherwise) by Python. If we put an
228- # idiomatic .close() or similar on, for example, a potentially large
229- # request body, we could implement it in terms of `del`.
230- except Err :
231- pass
232- else :
233- raise RuntimeError (
234- "Our use of send() to consume the previous request unexpectedly actually performed a send."
235- )
236-
237214 options = NextRequestOptions (timeout_ms = self .reuse_sandboxes_for_ms , extra = None )
238215 while True :
239216 pending_request = next_request (options )
@@ -254,9 +231,10 @@ def handle(self, request: Any, body: Any) -> None:
254231 if not result :
255232 break
256233 request , body = result
257- serve_wsgi_request (
258- request ,
259- body ,
260- self .wsgi_app ,
261- handle_errors = self .handle_errors ,
262- )
234+ with request :
235+ serve_wsgi_request (
236+ request ,
237+ body ,
238+ self .wsgi_app ,
239+ handle_errors = self .handle_errors ,
240+ )
0 commit comments