Skip to content

fix: enhance MessageBus by added lock and extractions. - #875

Open
StarsExpress wants to merge 4 commits into
mpfaffenberger:mainfrom
StarsExpress:fix-cross-thread-safety
Open

fix: enhance MessageBus by added lock and extractions.#875
StarsExpress wants to merge 4 commits into
mpfaffenberger:mainfrom
StarsExpress:fix-cross-thread-safety

Conversation

@StarsExpress

@StarsExpress StarsExpress commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Refs issue #438's 3rd point to adjust code_puppy/messaging/bus.py: MessageBus.

Part 1: add a lock for provide_response()

Originally, emit() has with self._lock: wrapping the code to add message into self._outgoing.

However, provide_response() didn't have it, so I let it have one as well — for wrapping the code to add command into self._incoming.

Part 2: refactor emit() and provide_response()

Same drop-oldest pattern duplicated in emit() and provide_response() - small DRY nit, extract _put_with_drop(queue, item).

A shared static method joins MessageBus now:

@staticmethod
def _put_item_into_queue(q: "queue.Queue[T]", item: T) -> None:
    """Put item into queue. If queue is full, use FIFO to drop oldest and retry."""

    try:
        q.put_nowait(item)

    except queue.Full:
        try:
            q.get_nowait()
            q.put_nowait(item)

        except queue.Empty:
            pass

How emit() calls _put_item_into_queue:

self._put_item_into_queue(self._outgoing, message)

How provide_response calls _put_item_into_queue:

self._put_item_into_queue(self._incoming, command)

Part 3: TypeVar("T") shared at module level

Since several methods need TypeVar("T") for type safety, the following definition moves to a place between imports and the start of MessageBus:

T = TypeVar("T") # Auto-detect variable type.

Refactored emit() and provide_response() as well.
@StarsExpress
StarsExpress force-pushed the fix-cross-thread-safety branch from b6f72a9 to 53c4c81 Compare August 28, 2026 14:54
@StarsExpress StarsExpress changed the title fix: enhance cross thread safety for MessageBus. fix: enhance MessageBus by added lock and refactored methods. Aug 28, 2026
@StarsExpress StarsExpress changed the title fix: enhance MessageBus by added lock and refactored methods. fix: enhance MessageBus by added lock and extractions. Aug 28, 2026
@StarsExpress
StarsExpress marked this pull request as ready for review August 28, 2026 15:41
@mpfaffenberger

Copy link
Copy Markdown
Owner

Locks make me very nervous.

@StarsExpress

Copy link
Copy Markdown
Contributor Author

Locks make me very nervous.

Haha I hear you — locks are usually where bugs go to hide. Plus, we all like freedom a lot 😅

This morning I ran some commands, and have these interesting but also tricky results:
messaging/bus.py::provide_response() — the part I added the new lock — has no production callers.

1. Command tracing

rg -n "from code_puppy.messaging.bus import|from \.\.messaging\.bus import|from \.bus import" code_puppy/ --type py gives this:

image

2. PyCharm's Find Usages

Checked on MessageBus.provide_response as well: only referenced within messaging/ itself and corresponding tests.

image

Observations

So MessageBus.provide_response is architecturally present but currently has zero production consumers.

This PR #875 is more like making sure the foundation is solid for whenever/if something actually gets wired up to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants