HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleresourcet-1.3.0Haskell2010

Control.Monad.Trans.Resource

Allocate resources which are guaranteed to be released.

For more information, see https://github.com/snoyberg/conduit/tree/master/resourcet#readme.

One point to note: all register cleanup actions live in the IO monad, not the main monad. This allows both more efficient code, and for monads to be transformed.

  • 7 types
  • 3 classes
  • 17 values
  • Packageresourcet-1.3.0
  • Exports27
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceResource.hs

Data types

3 declarations
newtypenewtype ResourceT (m :: Type -> Type) a
#

The Resource transformer. This transformer keeps track of all registered actions, and calls them upon exit (via runResourceT). Actions may be registered via register, or resources may be allocated atomically via allocate. allocate corresponds closely to bracket.

Releasing may be performed before exit via the release function. This is a highly recommended optimization, as it will ensure that scarce resources are freed early. Note that calling release will deregister the action, so that a release action will only ever be called once.

Since 0.3.0

Instances22MonadTrans, MonadRWS, MonadError, MonadReader, MonadState, MonadWriter, …
datadata ReleaseKey
#

A lookup key for a specific release action. This value is returned by register and allocate, and is passed to release.

Since 0.3.0

Unwrap

1 declaration
valuerunResourceT :: MonadUnliftIO m => ResourceT m a -> m a
#

Unwrap a ResourceT transformer, and call all registered release actions.

Note that there is some reference counting involved due to resourceForkIO. If multiple threads are sharing the same collection of resources, only the last call to runResourceT will deallocate the resources.

NOTE Since version 1.2.0, this function will throw a ResourceCleanupException if any of the cleanup functions throw an exception.

Check cleanup exceptions

Thrown when one or more cleanup functions themselves throw an exception during cleanup.

Constructors

Instances2Show, Exception

Special actions

2 declarations
valueresourceForkWith
  1. :: MonadUnliftIO m
  2. => IO () -> IO a
  3. -> ResourceT m ()
  4. -> ResourceT m a
#

Introduce a reference-counting scheme to allow a resource context to be shared by multiple threads. Once the last thread exits, all remaining resources will be released.

The first parameter is a function which will be used to create the thread, such as forkIO or async.

Note that abuse of this function will greatly delay the deallocation of registered resources. This function should be used with care. A general guideline:

If you are allocating a resource that should be shared by multiple threads, and will be held for a long time, you should allocate it at the beginning of a new ResourceT block and then call resourceForkWith from there.

Launch a new reference counted resource context using forkIO.

This is defined as resourceForkWith forkIO.

Note: Using regular forkIO inside of a ResourceT is inherently unsafe, since the forked thread may try access the resources of the parent after they are cleaned up. When you use resourceForkIO or resourceForkWith, ResourceT is made aware of the new thread, and will only cleanup resources when all threads finish. Other concurrency mechanisms, like concurrently or race, are safe to use.

If you encounter InvalidAccess exceptions ("The mutable state is being accessed after cleanup"), use of forkIO is a possible culprit.

Monad transformation

2 declarations
valuetransResourceT :: (m a -> n b) -> ResourceT m a -> ResourceT n b
#

Transform the monad a ResourceT lives in. This is most often used to strip or add new transformers to a stack, e.g. to run a ReaderT.

Note that this function is a slight generalization of hoist.

Since 0.3.0

Registering/releasing

6 declarations
valueallocate
  1. :: MonadResource m
  2. => IO a

    allocate

  3. -> (a -> IO ())

    free resource

  4. -> m (ReleaseKey, a)
#

Perform some allocation, and automatically register a cleanup action.

This is almost identical to calling the allocation and then registering the release action, but this properly handles masking of asynchronous exceptions.

Since 0.3.0

valueallocate_
  1. :: MonadResource m
  2. => IO a

    allocate

  3. -> IO ()

    free resource

  4. -> m ReleaseKey
#

Perform some allocation where the return value is not required, and automatically register a cleanup action.

allocate_ is to allocate as bracket_ is to bracket

This is almost identical to calling the allocation and then registering the release action, but this properly handles masking of asynchronous exceptions.

valuerelease :: MonadIO m => ReleaseKey -> m ()
#

Call a release action early, and deregister it from the list of cleanup actions to be performed.

Since 0.3.0

valueunprotect :: MonadIO m => ReleaseKey -> m (Maybe (IO ()))
#

Unprotect resource from cleanup actions; this allows you to send resource into another resourcet process and reregister it there. It returns a release action that should be run in order to clean resource or Nothing in case if resource is already freed.

Since 0.4.5

Type class/associated types

2 declarations
classclass MonadIO m => MonadResource (m :: Type -> Type) where
#

A Monad which allows for safe resource allocation. In theory, any monad transformer stack which includes a ResourceT can be an instance of MonadResource.

Note: runResourceT has a requirement for a MonadUnliftIO m monad, which allows control operations to be lifted. A MonadResource does not have this requirement. This means that transformers such as ContT can be an instance of MonadResource. However, the ContT wrapper will need to be unwrapped before calling runResourceT.

Since 0.3.0

Methods

Instances12MonadResource, …

Deprecated. Use MonadUnliftIO directly instead

Just use MonadUnliftIO directly now, legacy explanation continues:

A Monad which can be used as a base for a ResourceT.

A ResourceT has some restrictions on its base monad:

  • runResourceT requires an instance of MonadUnliftIO.

  • MonadResource requires an instance of MonadIO

Note that earlier versions of conduit had a typeclass ResourceIO. This fulfills much the same role.

Since 0.3.2

Low-level

Re-exports

1 declaration
classclass MonadIO m => MonadUnliftIO (m :: Type -> Type) where
#

Monads which allow their actions to be run in IO.

While MonadIO allows an IO action to be lifted into another monad, this class captures the opposite concept: allowing you to capture the monadic context. Note that, in order to meet the laws given below, the intuition is that a monad must have no monadic state, but may have monadic context. This essentially limits MonadUnliftIO to ReaderT and IdentityT transformers on top of IO.

Laws. For any function run provided by withRunInIO, it must meet the monad transformer laws as reformulated for MonadUnliftIO:

  • run . return = return
  • run (m >>= f) = run m >>= run . f

Instances of MonadUnliftIO must also satisfy the following laws:

Identity law

withRunInIO (\run -> run m) = m

Inverse law

withRunInIO (\_ -> m) = liftIO m

As an example of an invalid instance, a naive implementation of MonadUnliftIO (StateT s m) might be

withRunInIO inner =
  StateT $ \s ->
    withRunInIO $ \run ->
      inner (run . flip evalStateT s)

This breaks the identity law because the inner run m would throw away any state changes in m.

Instances4MonadUnliftIO

Internal state

6 declarations

A ResourceT internally is a modified ReaderT monad transformer holding onto a mutable reference to all of the release actions still remaining to be performed. If you are building up a custom application monad, it may be more efficient to embed this ReaderT functionality directly in your own monad instead of wrapping around ResourceT itself. This section provides you the means of doing so.

Create a new internal state. This state must be closed with closeInternalState. It is your responsibility to ensure exception safety. Caveat emptor!

Since 0.4.9

Reexport

1 declaration
classclass Monad m => MonadThrow (m :: Type -> Type) where
#

A class for monads in which exceptions may be thrown.

Instances should obey the following law:

throwM e >> x = throwM e

In other words, throwing an exception short-circuits the rest of the monadic computation.

Methods

  • throwM :: (HasCallStack, Exception e) => e -> m a

    Throw an exception. Note that this throws when this action is run in the monad m, not when it is applied. It is a generalization of Control.Exception's throwIO.

    Should satisfy the law:

    throwM e >> f = throwM e
Instances20MonadThrow, …