Skip to content

handle list changed size during iteration in websockets broadcast#1991

Closed
graingert wants to merge 10 commits intofastapi:masterfrom
graingert:patch-1
Closed

handle list changed size during iteration in websockets broadcast#1991
graingert wants to merge 10 commits intofastapi:masterfrom
graingert:patch-1

Conversation

@graingert
Copy link
Contributor

if you get a connect or disconnect during broadcast

@github-actions
Copy link
Contributor

📝 Docs preview for commit 0786806 at: https://5f4cf7560889c62bd66d666c--fastapi.netlify.app

@github-actions
Copy link
Contributor

📝 Docs preview for commit 26cab4e at: https://5f4d139bd2823d541cd7a6c2--fastapi.netlify.app

Copy link
Member

@Kludex Kludex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind explaining what you're trying to achieve? 🎉

class ConnectionManager:
def __init__(self):
self.active_connections: List[WebSocket] = []
self.active_connections: MutableSet[_IdEq(WebSocket)] = set()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.active_connections: MutableSet[_IdEq(WebSocket)] = set()
self.active_connections: MutableSet[_IdEq[WebSocket]] = set()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've suggested this, but you probably want to create a variable for it. Because on the add you want to use that new variable to something like: _newIdEq(websocket)

Copy link
Contributor Author

@graingert graingert Aug 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be better to drop the __eq__ from HTTPConnecttion

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kludex how did this pass the tests?

@codecov
Copy link

codecov bot commented Aug 31, 2020

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (e77ea63) 100.00% compared to head (563c486) 100.00%.
Report is 2363 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #1991   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          239       239           
  Lines         7079      7079           
=========================================
  Hits          7079      7079           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@graingert
Copy link
Contributor Author

Do you mind explaining what you're trying to achieve? 🎉

if you get a disconnect/connect during broadcast you can skip over sockets in the broadcast queue:

>>> x = [1, 2, 3, 4, 5, 6, 7]
>>> y = iter(x)
>>> next(y)
1
>>> next(y)
2
>>> next(y)
3
>>> x.remove(1)
>>> next(y)
5

@github-actions
Copy link
Contributor

📝 Docs preview for commit 465fd03 at: https://5f4d4b65e676710091ba6af8--fastapi.netlify.app

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
@github-actions
Copy link
Contributor

📝 Docs preview for commit 563c486 at: https://5f4d75a1479f1112f7a7d3c6--fastapi.netlify.app

@graingert
Copy link
Contributor Author

Starlette has a fix for this in 0.16.0 and so this PR needs to change

@tiangolo
Copy link
Member

tiangolo commented Sep 1, 2022

So, should anything else be done here @graingert or can we close it?


If you think there's something that still needs to be done here, I feel the code change and complexity is a bit daunting, I would try to find a way to make it as simple as possible, and easy to read for newcomers. And for example, I wouldn't use attrs but Pydantic, as everything is using Pydantic here, and FastAPI doesn't depend on attrs directly.

@graingert
Copy link
Contributor Author

The tutorial just needs to be updated to use a set instead

@github-actions github-actions bot removed the answered label Sep 1, 2022
@tiangolo tiangolo added the docs Documentation about how to use FastAPI label Jun 28, 2023
@alejsdev alejsdev added the p5 label Jan 16, 2024
@tiangolo
Copy link
Member

tiangolo commented Apr 2, 2024

As this won't be handled in this PR, I'm gonna go ahead and close it to clean up things. Thanks! ☕

@tiangolo tiangolo closed this Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation about how to use FastAPI p5

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants