Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Logging and Errors

CS2 stops sending updates to a server that answers with an error, so pygsi always answers with 200 OK, whatever goes wrong. It logs the problem instead, which means the log is where to look when something doesn’t work.

Logs

pygsi logs through Python’s logging module, under the logger name pygsi.server. At the DEBUG level, it also logs every raw payload as indented JSON, which helps when you want to see exactly what CS2 sent:

import logging

logging.basicConfig(level=logging.INFO)
logging.getLogger("pygsi.server").setLevel(logging.DEBUG)

The payloads are long and arrive often, so the example sends them to their own file.

Errors in Handlers

When a handler raises an exception, pygsi logs it with its traceback and carries on, so the other handlers and events for that payload still run. A payload that isn’t valid JSON, or doesn’t match the models, gets logged and dropped, and the stored state stays as it was.

Slow Handlers

pygsi runs the handlers for a payload before it answers CS2, and CS2 doesn’t send its next payload until it has that answer, which means no new updates arrive while a handler is busy. If the handlers take longer than the timeout in the config file (5 seconds in ours), CS2 counts the request as failed and sends the full state again next time.

Handlers should finish quickly for that reason. When one has slow work to do, like a request to a web API, it can start a task and return right away:

import asyncio

from pygsi import GSIServer, PlayerMatchStats

gsi = GSIServer(player_ids="76561198000000000")
background: set[asyncio.Task[None]] = set()


async def post_kill(kills: int) -> None:
    await asyncio.sleep(3)  # stands in for a slow request
    print(f"Posted {kills} kills")


@gsi.on_local_player_kill
async def kill(
    player_id: str, old: PlayerMatchStats | None, new: PlayerMatchStats
) -> None:
    task = asyncio.create_task(post_kill(new.kills))
    background.add(task)
    task.add_done_callback(background.discard)

The background set keeps a reference to each task until it’s done, because asyncio only keeps a weak one, and a task with no other reference can disappear before it finishes.