Conversation
trivenay
reviewed
Sep 25, 2026
vip-amzn
force-pushed
the
feat/pre-fork-hooks
branch
from
September 25, 2026 10:52
bbb4f33 to
e42e48e
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add pre-fork hooks for multi-concurrent mode
Summary
Adds
@register_pre_fork, a hook that runs once in the parent process beforeworker processes are started, for functions running in multi-concurrent mode
(Lambda Managed Instances).
Motivation
In multi-concurrent mode the runtime interface client starts N worker processes,
and each worker imports the handler module independently. Module-level
initialization therefore runs N times, once per worker.
That is usually fine, but it is wasteful or outright incorrect for one-time setup
whose result is shared outside the process:
/tmp— N workers download thesame object concurrently and race on the same path.
and compete for the same port.
duplicated work, and the workers can observe a half-written file.
A pre-fork hook lets that work happen exactly once, before any worker exists.
Usage
Behaviour
(importing it is what registers them) and before any worker is started.
whatever concurrency the execution environment is configured with.
Hooks cannot share in-memory state with workers
Worker processes re-import the handler module independently, so a pre-fork hook
cannot hand in-memory objects to workers. Hooks are for external side
effects — a subprocess, a file in
/tmp, a warmed local service. Anything aworker needs in memory must still be created in the worker.
Failure handling
A hook that raises fails initialization:
Runtime.PreForkError,identifiable,
This is deliberate. If a hook could not establish the precondition its workers
depend on — the model was never downloaded, the sidecar never came up — starting
the pool anyway would turn one clear initialization failure into a stream of
confusing per-invocation failures.
Testing
the parent, and the failure path (reported error type, remaining hooks
skipped, no workers started).