Skip to main content

Isolate OOM-prone functions in a separate process, raise exceptions on SIGKILL or segfault

Project description

bombsquad

Isolate OOM-prone functions in a separate process, raise exceptions on SIGKILL or segfault.

Methods that cause out-of-memory errors (OOM) or segfaults are killed by the OS immediately, crashing the interpreter without raising an exception.

To solve this problem, the bombsquad package provides the @bombsquad decorator. This runs the decorated function in a separate, isolated process. If that process is terminated by the OS (e.g. SIGKILL), bombsquad catches the exit code and raises a NonzeroExitcode exception in your main process.

Normal Python exceptions raised within the function are propagated back to the parent process transparently.

Installation

pip install bombsquad

Quick Start

1. Trapping a Crash

Protect your main application from dangerous functions.

import os
import signal
from bombsquad import bombsquad, NonzeroExitcode

@bombsquad
def risky_business():
    print("Running risky code...")
    # Simulate a hard crash (like an OOM killer)
    os.kill(os.getpid(), signal.SIGKILL)

try:
    risky_business()
except NonzeroExitcode as e:
    print(f"Caught crash! Process exited with code {e.exitcode}")

2. Propagating Exceptions

Standard exceptions work exactly as expected.

@bombsquad
def divide(a, b):
    return a / b

try:
    divide(10, 0)
except ZeroDivisionError:
    print("Caught division by zero from child process.")

Advanced Configuration

The decorator accepts arguments to tune performance and safety.

@bombsquad(start_method="spawn", backend="file")
def massive_processing(data):
    ...

Parameters

Argument Options Default Description
start_method "spawn", "fork" "spawn" How the child process is created.
backend "file", "queue" "file" How data is returned to the parent.

Choosing a backend

  • "file" (Default, Recommended):

    • Mechanism: Serializes results to a temporary file on disk.
    • Pros: Supports unlimited return sizes (limited only by disk space).
    • Cons: Slower due to disk I/O.
    • Use for: Returning large arrays, datasets, or huge strings.
  • "queue":

    • Mechanism: Uses multiprocessing.Queue (memory pipes).
    • Pros: Faster (in-memory transfer).
    • Cons: Limited to ~4GB payloads (Python pickle limit).
    • Use for: Returning small results (status codes, metrics, small dicts).

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bombsquad-0.1.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bombsquad-0.1.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file bombsquad-0.1.1.tar.gz.

File metadata

  • Download URL: bombsquad-0.1.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for bombsquad-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7f57a7031c29fa465e02a2b86b07c9fd4661fa4a2ed9e5800c8244c048923e41
MD5 f88ff6790e13ce013280d89115dcc283
BLAKE2b-256 ed4daea4b98520db2e4a6d24420a0ec2a3011d6f1ddd70b5a9c6bf0bd105a979

See more details on using hashes here.

File details

Details for the file bombsquad-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: bombsquad-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for bombsquad-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5b2f0018ca73c0898dcffc977a5f18be9ed52072b4c8a985a8419ec047613564
MD5 d140a6f8c87025985c6928687356c6fc
BLAKE2b-256 595142ce9fd1152b69d9bb208d692d78e957cfd3f23d1d25aadad0576ba1536a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page