Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-90467: StreamReaderProtocol - add strong reference to created task #96323

Conversation

python273
Copy link
Contributor

gh-90467: StreamReaderProtocol - add strong reference to created task

@gvanrossum
Copy link
Member

Oh now I have to research whether it’s possible to have more than one task (like the other PR #30505 assumed) and if so whether it’s necessary to keep all of them alive, or just the most recent one.

@python273
Copy link
Contributor Author

hmm, StreamReaderProtocolis created once per connection

def factory():
reader = StreamReader(limit=limit, loop=loop)
protocol = StreamReaderProtocol(reader, client_connected_cb,
loop=loop)
return protocol
return await loop.create_server(factory, host, port, **kwds)

and

     State machine of calls: 
  
       start -> CM [-> DR*] [-> ER?] -> CL -> end 
  
     * CM: connection_made() 
     * DR: data_received() 
     * ER: eof_received() 
     * CL: connection_lost() 

class Protocol(BaseProtocol):
"""Interface for stream protocol.
The user should implement this interface. They can inherit from
this class but don't need to. The implementations here do
nothing (they don't raise exceptions).
When the user wants to requests a transport, they pass a protocol
factory to a utility function (e.g., EventLoop.create_connection()).
When the connection is made successfully, connection_made() is
called with a suitable transport object. Then data_received()
will be called 0 or more times with data (bytes) received from the
transport; finally, connection_lost() will be called exactly once
with either an exception object or None as an argument.
State machine of calls:
start -> CM [-> DR*] [-> ER?] -> CL -> end
* CM: connection_made()
* DR: data_received()
* ER: eof_received()
* CL: connection_lost()
"""

@gvanrossum
Copy link
Member

Yeah, that sounds about right. I'll give the author and reviewers of the other PR some time to find a flaw in this argument. In the meantime I'm going to press the Update branch button.

@gvanrossum
Copy link
Member

Also you still need to add a blurb -- see the failing test for instructions.

@kumaraditya303 kumaraditya303 added type-bug An unexpected behavior, bug, or error topic-asyncio needs backport to 3.10 only security fixes needs backport to 3.11 only security fixes labels Aug 27, 2022
@bharel
Copy link
Contributor

bharel commented Aug 27, 2022

Don't see a flaw.

I'm not entirely sure why I thought it might be called multiple times for concurrent connections. A protocol should be used for only one connection and the state machine is not per connection but per protocol.

As long as the end user plays according to the book it seems like I made a mistake.

The test simply leaks until the cleanup function removes the loop and the GC activates. You can choose whether to include it or not.

Please add Simon to the acks :-) His original PR was the same until I modified it.

@python273 python273 force-pushed the fix-stream-reader-protocol-task-strong-reference branch from dc7c144 to 83a816d Compare August 27, 2022 15:10
@gvanrossum gvanrossum merged commit e860e52 into python:main Aug 27, 2022
@miss-islington
Copy link
Contributor

Thanks @python273 for the PR, and @gvanrossum for merging it 🌮🎉.. I'm working now to backport this PR to: 3.10, 3.11.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-96344 is a backport of this pull request to the 3.11 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 27, 2022
@bedevere-bot bedevere-bot removed the needs backport to 3.10 only security fixes label Aug 27, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 27, 2022
@bedevere-bot
Copy link

GH-96345 is a backport of this pull request to the 3.10 branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-asyncio type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants