def test_callback_dictionary_modification(temp_dir: str):
    """Test that the event stream can handle dictionary modification during iteration.

    This test verifies that the fix for the 'dictionary changed size during iteration' error works.
    The test adds a callback that adds a new callback during iteration, which would cause an error
    without the fix.
    """
    file_store = get_file_store('local', temp_dir)
    event_stream = EventStream('callback_test', file_store)

    # Track callback execution
    callback_executed = [False, False, False]

    # Define a callback that will be added during iteration
    def callback_added_during_iteration(event):
        callback_executed[2] = True

    # First callback that will be called
    def callback1(event):
        callback_executed[0] = True
        # This callback will add a new callback during iteration
        # Without our fix, this would cause a "dictionary changed size during iteration" error
        event_stream.subscribe(
            EventStreamSubscriber.TEST, callback_added_during_iteration, 'callback3'
        )

    # Second callback that will be called
    def callback2(event):
        callback_executed[1] = True

    # Subscribe both callbacks
    event_stream.subscribe(EventStreamSubscriber.TEST, callback1, 'callback1')
    event_stream.subscribe(EventStreamSubscriber.TEST, callback2, 'callback2')

    # Add an event to trigger callbacks
    event_stream.add_event(NullObservation('test'), EventSource.AGENT)

    # Give some time for the callbacks to execute
    time.sleep(0.5)

    # Verify that the first two callbacks were executed
    assert callback_executed[0] is True, 'First callback should have been executed'
    assert callback_executed[1] is True, 'Second callback should have been executed'

    # The third callback should not have been executed for this event
    # since it was added during iteration
    assert callback_executed[2] is False, (
        'Third callback should not have been executed for this event'
    )

    # Add another event to trigger all callbacks including the newly added one
    callback_executed = [False, False, False]  # Reset execution tracking
    event_stream.add_event(NullObservation('test2'), EventSource.AGENT)

    # Give some time for the callbacks to execute
    time.sleep(0.5)

    # Now all three callbacks should have been executed
    assert callback_executed[0] is True, 'First callback should have been executed'
    assert callback_executed[1] is True, 'Second callback should have been executed'
    assert callback_executed[2] is True, 'Third callback should have been executed'

    # Clean up
    event_stream.close()
Test that the event stream can handle dictionary modification during iteration.

    This test verifies that the fix for the 'dictionary changed size during iteration' error works.
    The test adds a callback that adds a new callback during iteration, which would cause an error
    without the fix.
    
tests/unit/test_event_stream.py