Skip to content

handle list changed size during iteration in websockets broadcast - #1991

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

graingert wants to merge 10 commits into
fastapi:masterfrom
graingert:patch-1

Conversation

@graingert

Copy link
Copy Markdown
Contributor

if you get a connect or disconnect during broadcast

Comment thread docs_src/websockets/tutorial003.py
@github-actions

Copy link
Copy Markdown
Contributor

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

@github-actions

Copy link
Copy Markdown
Contributor

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

@Kludex Kludex left a comment

Copy link
Copy Markdown
Member

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? 🎉

Comment thread docs_src/websockets/tutorial003.py Outdated
class ConnectionManager:
def __init__(self):
self.active_connections: List[WebSocket] = []
self.active_connections: MutableSet[_IdEq(WebSocket)] = set()

Copy link
Copy Markdown
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
Copy Markdown
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)

@graingert graingert Aug 31, 2020

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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?

Comment thread docs_src/websockets/tutorial003.py Outdated
@codecov

codecov Bot commented Aug 31, 2020

Copy link
Copy Markdown

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.

Comment thread docs_src/websockets/tutorial003.py Outdated
@graingert

Copy link
Copy Markdown
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
Copy Markdown
Contributor

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

Co-authored-by: Marcelo Trylesinski <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

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

@graingert

Copy link
Copy Markdown
Contributor Author

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

@tiangolo

tiangolo commented Sep 1, 2022

Copy link
Copy Markdown
Member

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
Copy Markdown
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

tiangolo commented Apr 2, 2024

Copy link
Copy Markdown
Member

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