Skip to content

Fix RuntimeError in get_block_device_mapping - #10185

Open
jeblair wants to merge 1 commit into
getmoto:masterfrom
jeblair:fix-get-block-device-mapping
Open

Fix RuntimeError in get_block_device_mapping#10185
jeblair wants to merge 1 commit into
getmoto:masterfrom
jeblair:fix-get-block-device-mapping

Conversation

@jeblair

@jeblair jeblair commented Aug 13, 2026

Copy link
Copy Markdown

In a multi-threaded situation, the block_device_mapping dictionary can change in one thread while another thread is iterating over it using get_block_device_mapping. Fix that by returning a list instead of an iterator (as we do for the network_interfaces property below).

Last few lines of a sample traceback:

  File "/home/zuul/src/opendev.org/zuul/zuul/.nox/tests-3-14/lib/python3.14/site-packages/moto/core/serialize.py", line 1282, in __call__
    value = get_value(obj, possible_key, MISSING)
  File "/home/zuul/src/opendev.org/zuul/zuul/.nox/tests-3-14/lib/python3.14/site-packages/moto/core/utils.py", line 483, in get_value
    return _get_value_for_key(obj, key, default)
  File "/home/zuul/src/opendev.org/zuul/zuul/.nox/tests-3-14/lib/python3.14/site-packages/moto/core/utils.py", line 496, in _get_value_for_key
    return getattr(obj, str(key), default)
  File "/home/zuul/src/opendev.org/zuul/zuul/.nox/tests-3-14/lib/python3.14/site-packages/moto/ec2/models/instances.py", line 334, in block_device_mappings
    for device_name, block in self.get_block_device_mapping:
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

RuntimeError: dictionary changed size during iteration

In a multi-threaded situation, the block_device_mapping dictionary
can change in one thread while another thread is iterating over it
using get_block_device_mapping.  Fix that by returning a list
instead of an iterator (as we do for the network_interfaces
property below).

Last few lines of a sample traceback:

  File "/home/zuul/src/opendev.org/zuul/zuul/.nox/tests-3-14/lib/python3.14/site-packages/moto/core/serialize.py", line 1282, in __call__
    value = get_value(obj, possible_key, MISSING)
  File "/home/zuul/src/opendev.org/zuul/zuul/.nox/tests-3-14/lib/python3.14/site-packages/moto/core/utils.py", line 483, in get_value
    return _get_value_for_key(obj, key, default)
  File "/home/zuul/src/opendev.org/zuul/zuul/.nox/tests-3-14/lib/python3.14/site-packages/moto/core/utils.py", line 496, in _get_value_for_key
    return getattr(obj, str(key), default)
  File "/home/zuul/src/opendev.org/zuul/zuul/.nox/tests-3-14/lib/python3.14/site-packages/moto/ec2/models/instances.py", line 334, in block_device_mappings
    for device_name, block in self.get_block_device_mapping:
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: dictionary changed size during iteration
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.

1 participant