Skip to content

Commit 16e1090

Browse files
Python: Add sandbox import notification policy documentation (#3973)
* Add sandbox import notification policy documentation * Update docs/develop/python/python-sdk-sandbox.mdx Co-authored-by: Jwahir Sundai <[email protected]> * Update example to use both warning types instead of using the same option twice --------- Co-authored-by: Jwahir Sundai <[email protected]>
1 parent ad75eae commit 16e1090

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

docs/develop/python/python-sdk-sandbox.mdx

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@ To skip a sandbox environment for a Worker, set the `workflow_runner` keyword a
100100
When creating the Worker, the `workflow_runner` defaults to [`SandboxedWorkflowRunner()`](https://python.temporal.io/temporalio.worker.workflow_sandbox.SandboxedWorkflowRunner.html).
101101
The `SandboxedWorkflowRunner` init accepts a `restrictions` keyword argument that defines a set of restrictions to apply to this sandbox.
102102

103-
The [`SandboxRestrictions`](https://python.temporal.io/temporalio.worker.workflow_sandbox.SandboxRestrictions.html) dataclass is immutable and contains three fields that can be customized, but only two have notable values.
103+
The [`SandboxRestrictions`](https://python.temporal.io/temporalio.worker.workflow_sandbox.SandboxRestrictions.html) dataclass is immutable and contains four fields that can be customized, but only three have notable values.
104104

105105
- [`passthrough_modules`](https://python.temporal.io/temporalio.worker.workflow_sandbox.SandboxRestrictions.html#passthrough_modules)
106106
- [`invalid_modules_members`](https://python.temporal.io/temporalio.worker.workflow_sandbox.SandboxRestrictions.html#invalid_module_members)
107+
- [`import_notification_policy`](https://python.temporal.io/temporalio.worker.workflow_sandbox.SandboxRestrictions.html#import_notificaton_policy)
107108

108109
### Passthrough modules
109110

@@ -195,6 +196,55 @@ my_restrictions = dataclasses.replace(
195196
my_worker = Worker(..., workflow_runner=SandboxedWorkflowRunner(restrictions=my_restrictions))
196197
```
197198

199+
### Import Notification Policy
200+
201+
The sandbox's import notification policy specifies how the sandbox behaves when it imports modules in a way that may be unintentional. It covers two common scenarios: dynamic imports and enforcing module passthrough. Each can be controlled independently.
202+
203+
A dynamic import occurs when a module is imported after the Workflow is loaded into the sandbox. These imports are often invisible and, if they don't do anything restricted by the sandbox, cause memory overhead. By default the [`WARN_ON_DYNAMIC_IMPORT`](https://python.temporal.io/temporalio.workflow.SandboxImportNotificationPolicy.html#WARN_ON_DYNAMIC_IMPORT) policy setting is enabled and a warning will be emitted when one of these imports occurs.
204+
205+
The other notable policy settings apply when a module is imported into the sandbox that was not passed through. These settings are disabled by default and must be explicitly turned on.
206+
The [`WARN_ON_UNINTENTIONAL_PASSTHROUGH`](https://python.temporal.io/temporalio.workflow.SandboxImportNotificationPolicy.html#WARN_ON_UNINTENTIONAL_PASSTHROUGH) setting emits a warning when a module not included in the [passthrough modules](#passthrough-modules) list.
207+
208+
Similarly, the [`RAISE_ON_UNINTENTIONAL_PASSTHROUGH`](https://python.temporal.io/temporalio.workflow.SandboxImportNotificationPolicy.html#RAISE_ON_UNINTENTIONAL_PASSTHROUGH) setting will raise an error when an non-passed-through module is imported.
209+
210+
The import notification policy can be set for specific imports by using [`sandbox_import_notification_policy`](https://python.temporal.io/temporalio.workflow.unsafe.html#sandbox_import_notification_policy) context manager.
211+
212+
```python
213+
# my_workflow_file.py
214+
215+
from temporalio import workflow
216+
217+
with workflow.unsafe.sandbox_import_notification_policy(
218+
workflow.SandboxImportNotificationPolicy.SILENT
219+
):
220+
import pydantic
221+
222+
@workflow.defn
223+
class MyWorkflow:
224+
# ...
225+
```
226+
227+
This can also be done at worker creation time by customizing the runner's restrictions.
228+
229+
```python
230+
# my_worker_file.py
231+
232+
from temporalio.worker import Worker
233+
from temporalio.worker.workflow_sandbox import SandboxedWorkflowRunner, SandboxRestrictions
234+
235+
my_worker = Worker(
236+
...,
237+
workflow_runner=SandboxedWorkflowRunner(
238+
restrictions=SandboxRestrictions.default.with_import_notification_policy(
239+
workflow.SandboxImportNotificationPolicy.WARN_ON_DYNAMIC_IMPORT | workflow.SandboxImportNotificationPolicy.WARN_ON_UNINTENTIONAL_PASSTHROUGH
240+
)
241+
)
242+
)
243+
```
244+
245+
The [`sandbox_import_notification_policy`](https://python.temporal.io/temporalio.workflow.unsafe.html#sandbox_import_notification_policy) context manager will always be respected if used in combination with the restrictions customization.
246+
247+
198248
For more information on the Python sandbox, see the following resources.
199249

200250
- [Python SDK README](https://github.com/temporalio/sdk-python)

0 commit comments

Comments
 (0)