@@ -23,6 +23,7 @@ module Servant.Server.Internal
23 , module Servant.Server.Internal.ServantErr
23 , module Servant.Server.Internal.ServantErr
26+import Control.Exception (finally)
26import Control.Monad.Trans (liftIO)
27import Control.Monad.Trans (liftIO)
27import qualified Data.ByteString as B
28import qualified Data.ByteString as B
28import qualified Data.ByteString.Char8 as BC8
29import qualified Data.ByteString.Char8 as BC8
@@ -400,11 +401,13 @@ instance HasServer Raw context where
400 type ServerT Raw m = Application
401 type ServerT Raw m = Application
402 route Proxy _ rawApplication = RawRouter $ \ env request respond -> do
403 route Proxy _ rawApplication = RawRouter $ \ env request respond -> do
403− (r, _) <- runDelayed rawApplication env request
404+ (r, cleanup) <- runDelayed rawApplication env request
404− case r of
405+ go r request respond `finally` cleanup
407+ where go r request respond = case r of
405− Route app -> app request (respond . Route)
408+ Route app -> app request (respond . Route)
406− Fail a -> respond $ Fail a
409+ Fail a -> respond $ Fail a
407− FailFatal e -> respond $ FailFatal e
410+ FailFatal e -> respond $ FailFatal e
409-- | If you use 'ReqBody' in one of the endpoints for your API,
412-- | If you use 'ReqBody' in one of the endpoints for your API,
410-- this automatically requires your server-side handler to be a function
413-- this automatically requires your server-side handler to be a function