Skip to content

[Bug]: Docker API hook manager crashes for all kind of user-provided hooks #1878

@przemekmatusiak

Description

@przemekmatusiak

crawl4ai version

v8.0.0

Expected Behavior

Providing a hook via API request body should not cause hook_manager.py error before the hook code execution.

Current Behavior

Even the simplest hook, like this one

async def hook(page, context, **kwargs):
    return page

is causing the hook validation errors in the logs:

2026-03-28 09:32:15,743 - hook_manager - ERROR - Hook compilation failed for on_page_context_created: __import__ not found
2026-03-28 09:32:15,743 - hook_manager - WARNING - Hook validation errors: [{'hook_point': 'on_page_context_created', 'error': 'Failed to compile hook function - check syntax and structure', 'code_preview': '\nasync def hook(page, context, **kwargs):\n    return page\n'}]
2026-03-28 09:32:15,743 - api - INFO - Hooks attachment status: failed

hooks part of the API response:

(...)
"hooks": {
        "status": {
            "status": "failed",
            "attached_hooks": [],
            "validation_errors": [
                {
                    "hook_point": "on_page_context_created",
                    "error": "Failed to compile hook function - check syntax and structure",
                    "code_preview": "\nasync def hook(page, context, **kwargs):\n    return page\n"
                }
            ],
            "total_hooks_provided": 1,
            "successfully_attached": 0,
            "failed_validation": 1
        },
        "execution_log": [],
        "errors": [
            {
                "hook_point": "on_page_context_created",
                "error": "Failed to compile hook: __import__ not found",
                "type": "compilation_error",
                "traceback": "Traceback (most recent call last):\n  File \"/app/hook_manager.py\", line 144, in compile_hook\n    exec(\"import asyncio\", namespace)\n  File \"<string>\", line 1, in <module>\nImportError: __import__ not found\n"
            }
        ],
        "summary": {
            "total_executions": 0,
            "successful": 0,
            "failed": 0,
            "timed_out": 0,
            "success_rate": 0,
            "total_errors": 1
        }
    }
}

Decoded traceback:

Traceback (most recent call last):
  File "/app/hook_manager.py", line 144, in compile_hook
    exec("import asyncio", namespace)
  File "<string>", line 1, in <module>
ImportError: __import__ not found

I think this is caused by the change in #1712, which removed the import ... option for hooks. However, in the next lines, the hook_manager tries to import some commonly used libraries, and asyncio appears to be the first.

Is this reproducible?

Yes

Inputs Causing the Bug

curl -X POST \
  http://localhost:11235/crawl \
  -H 'Content-Type: application/json' \
  -d '{
    "urls": ["https://www.reddit.com/r/devops/comments/1s51xj8/docker_vs_firecracker_for_browser_sandboxing/"],
    "hooks": {"timeout": 30, "code": {"on_page_context_created": "\nasync def hook(page, context, **kwargs):\n    return page\n"} }
  }'

Steps to Reproduce

Code snippets

                'getattr', 'hasattr', 'setattr', 'callable', 'iter', 'next',
                '__import__', '__build_class__'  # Required for exec        ---- #⚠️ '__import__' removed in v0.8.0
                '__build_class__'  # Required for class definitions in exec
            ]

            for name in allowed_builtins:
                if hasattr(builtins, name):
                    safe_builtins[name] = getattr(builtins, name)

            namespace = {
                '__name__': f'user_hook_{hook_point}',
                '__builtins__': safe_builtins
            }

            # Add commonly needed imports
            exec("import asyncio", namespace)        ---- #⚠️ crashes here
            exec("import json", namespace)

OS

Linux

Python version

3.12.13

Browser

No response

Browser version

No response

Error logs & Screenshots (if applicable)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    ⚙️ In-progressIssues, Features requests that are in Progress🐞 BugSomething isn't working📌 Root causedidentified the root cause of bug

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions