Network, file, compression, indexing, and local-server work must not block the game thread. Every result needs limits, validation, and a context check before it touches live game state.

Worker-to-game-thread pattern

Pseudocode: this handoff sketch omits imports and helper implementations. It does not supply transport limits, a concurrency cap, cancellation, a context-identity policy or a completion receipt.

import logging

_LOGGER = logging.getLogger(__name__)

def request_data(context):
    worker = threading.Thread(target=_request_worker, args=(context,))
    worker.daemon = True
    worker.start()

def _request_worker(context):
    try:
        payload = download_with_limits(
            timeout=5.0,
            max_bytes=1024 * 1024
        )
        result = validate_payload(payload)
        error = None
    except Exception as exc:
        _LOGGER.exception('Background request failed.')
        result = None
        error = repr(exc)

    BigWorld.callback(
        0.0,
        lambda: apply_download_result(context, result, error)
    )

def apply_download_result(context, result, error):
    if not context_is_current(context):
        return
    if error is not None:
        _LOGGER.warning('Request failed: %s', error)
        return
    update_live_state(result)

Each call starts a separate daemon and retains no thread/request/callback handle. context and the result are captured by reference, without a snapshot or primitive-data gate. Helper names such as download_with_limits and validate_payload express required work; their signatures do not implement a timeout, byte cap or schema.

Scheduling occurs outside the download/validation catch and its returned ID is discarded. Worker return therefore acknowledges neither callback execution nor successful application. The receiver can drop stale context, and context/application exceptions have no local recovery or completion signal. Supply those policies in the actual owner.

Record unexpected failures inside the original exception handler before forwarding an error value or raising later. A later error message alone cannot reconstruct the original failing stack. Keep completion signals, context checks and delivery policy unchanged; see traceback-bearing logs. The diagnostic branches were checked with inert scheduling and in-memory logging substitutes, without real threads, waits, network requests or game-thread execution.

Bound every external operation

  • Connection and read timeout
  • Maximum response and decompressed size
  • Recognized content type and schema
  • Bounded retry count with backoff
  • Request identity and current-context validation
  • Secret-safe diagnostics

Read by topic

Choose a focused chapter. Complete examples and reference details are preserved on their topic pages.