Skip to content

Possible bug when running sync in free-threading python #1662

@ismaelorenstein

Description

@ismaelorenstein

Hi,

I'm using websockets with free-threading python (3.14rc3) - ie with GIL disabled - and I occasionally have this runtime error when closing a connection:

  File "/home/isma/bigrob/bigrob/api/poly.py", line 801, in thread_close_ws_session                  
session.ws_session.close()                                                                                                                                                                                                 ~~~~~~~~~~~~~~~~~~~~~~~~^^                                                                                                                                                                                               File "/home/isma/bigrob/.venv/lib/python3.14t/site-packages/websockets/sync/connection.py", line 588, in close                                                                                                               with self.send_context():                                                                                                                                                                                                       ~~~~~~~~~~~~~~~~~^^         
  File "/home/isma/.local/share/uv/python/cpython-3.14.0rc3+freethreaded-linux-x86_64-gnu/lib/python3.14t/contextlib.py", line 148, in __exit__                                                                            
    next(self.gen)                                                                                                                                                                                                         
    ~~~~^^^^^^^^^^                                                                                                                                                                                                         
  File "/home/isma/bigrob/.venv/lib/python3.14t/site-packages/websockets/sync/connection.py", line 1009, in send_context                                                                                                   
    self.close_socket()                                                                                                                                                                                                    
    ~~~~~~~~~~~~~~~~~^^                                                                                                                                                                                                    
  File "/home/isma/bigrob/.venv/lib/python3.14t/site-packages/websockets/sync/connection.py", line 1072, in close_socket                                                                                                   
    self.acknowledge_pending_pings()                                                                                                                                                                                       
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^                                                                                                                                                                                       
  File "/home/isma/bigrob/.venv/lib/python3.14t/site-packages/websockets/sync/connection.py", line 741, in acknowledge_pending_pings                                                   
    for pong_waiter, _ping_timestamp, ack_on_close in self.pong_waiters.values():                                                                                                                                          
                                                      ~~~~~~~~~~~~~~~~~~~~~~~~^^                                                                                                                                           
RuntimeError: dictionary changed size during iteration          

Hasn't been easy to replicate it - happens rarely but just flagging it in case it is something you know the answer already

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions