Repository navigation
Description
What are you really trying to do?
Run a single-file Temporal program, with the workflow, the worker and the starter all in the script being run, using the sandbox with SandboxRestrictions.default.with_passthrough_all_modules().
Describe the bug
If the workflow class is defined in __main__, creating the worker with passthrough_all_modules fails with:
RuntimeError: Failed validating workflow InMain
caused by ModuleNotFoundError: No module named '__temporal_main__'
The same workflow works with the default restrictions, and a workflow in a regular module works with passthrough_all_modules. Only this combination fails:
| Restrictions | Workflow in other_module |
Workflow in __main__ |
|---|---|---|
SandboxRestrictions.default |
works | works |
.with_passthrough_all_modules() |
works | fails |
Expected behavior: the workflow runs, as it does in the other three cases. The README says passthrough_all_modules "can be set" whenever imports are deterministic, and lists no exceptions (README.md L1208-1216 @ 1.36.1). The README also defines workflows in __main__ in its own examples (DebugMeWorkflow, L1263 onward).
Cause
- The sandbox renames a workflow module named
__main__to__temporal_main__(_runner.pyL135-142). _maybe_passthrough_moduleexempts the workflow module from pass-through ("Workflow modules are intentionally reloaded"), but only inside the branch for modules that are not configured as pass-through (_importer.pyL316-325).- With
passthrough_all_modules,module_configured_passthrough()returnsTruefor every name (L288-294). The exemption is skipped and the importer callsimportlib.import_module("__temporal_main__")on the host, where no such module exists (L346). - The special case that loads
__temporal_main__from the original__main__file (L247-249) is never reached.
Explicitly listing "__temporal_main__" in passthrough_modules fails the same way, so the trigger is "__temporal_main__ is configured as pass-through", whichever way that happens.
main (at dd1d491) has the same logic, now through _is_passed_through() (_importer.py L344-377 @ main). I read this code on main but did not run it there.
Possible fix
When __temporal_main__ is configured as pass-through, pass through the host's __main__. This is consistent with how a regular workflow module is treated under passthrough_all_modules: passed through, not reloaded. I tested it as a monkeypatch on 1.36.1, and all four cases in the table then return "hello":
def _maybe_passthrough_module(self, name):
if name == "__temporal_main__" and self.module_configured_passthrough(name):
return _thread_local_sys_modules.orig["__main__"]
... # existing logicHow did you hit this issue?
I was writing a minimal single-file example while measuring how much boilerplate a trivial workflow needs. This isn't production impact, but single-file scripts are the most common shape for examples, tutorials and prototypes.
Minimal Reproduction
There are two files in the same directory. Run temporal server start-dev, then python repro.py.
other_module.py:
from temporalio import workflow
@workflow.defn
class InModule:
@workflow.run
async def run(self) -> str:
return "hello"repro.py:
import asyncio
import uuid
from temporalio import workflow
from temporalio.client import Client
from temporalio.worker import Worker
from temporalio.worker.workflow_sandbox import SandboxedWorkflowRunner, SandboxRestrictions
from other_module import InModule
@workflow.defn
class InMain:
@workflow.run
async def run(self) -> str:
return "hello"
async def run_case(client: Client, restrictions: SandboxRestrictions, wf: type) -> str:
task_queue = f"repro-{uuid.uuid4()}"
try:
async with Worker(
client,
task_queue=task_queue,
workflows=[wf],
workflow_runner=SandboxedWorkflowRunner(restrictions=restrictions),
):
return await client.execute_workflow(wf.run, id=task_queue, task_queue=task_queue)
except Exception as err:
return f"{type(err).__name__}: {err} <- {type(err.__cause__).__name__}: {err.__cause__}"
async def main() -> None:
client = await Client.connect("localhost:7233")
for name, restrictions in [
("default", SandboxRestrictions.default),
("passthrough_all", SandboxRestrictions.default.with_passthrough_all_modules()),
]:
for wf in (InModule, InMain):
print(f"{name:16} {wf.__module__:13} {await run_case(client, restrictions, wf)}")
if __name__ == "__main__":
asyncio.run(main())Output:
default other_module hello
default __main__ hello
passthrough_all other_module hello
passthrough_all __main__ RuntimeError: Failed validating workflow InMain <- ModuleNotFoundError: No module named '__temporal_main__'
Environment/Versions
- OS and processor: Linux x86_64 (kernel 7.0.0-1017-aws)
- SDK:
temporalio1.36.1 (latest on PyPI), Python 3.11.18 - Server: Temporal CLI 1.10.1 dev server (Server 1.32.2). The failure happens during worker validation, before any workflow is sent to the server.
- No Docker or Kubernetes, and not built from source.
Additional context
I searched for duplicates in issues and PRs ("temporal_main", "passthrough_all_modules", "sandbox main") and found none. Related but different: #691 (the feature request that added passthrough_all_modules).
Activity