-
Notifications
You must be signed in to change notification settings - Fork 565
fix eof_received #661
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
Open
dennissheng
wants to merge
1
commit into
MagicStack:master
Choose a base branch
from
dennissheng:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix eof_received #661
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import asyncio | ||
import ssl | ||
import threading | ||
import time | ||
import unittest | ||
|
||
from uvloop import _testbase as tb | ||
|
||
|
||
class TestCloseNotify(tb.SSLTestCase, tb.UVTestCase): | ||
|
||
ONLYCERT = tb._cert_fullname(__file__, 'ssl_cert.pem') | ||
ONLYKEY = tb._cert_fullname(__file__, 'ssl_key.pem') | ||
|
||
PAYLOAD_SIZE = 1024 * 50 | ||
TIMEOUT = 10 | ||
|
||
HELLO_MSG = b'A' * PAYLOAD_SIZE | ||
END_MSG = b'THE END' | ||
|
||
class ClientProto(asyncio.Protocol): | ||
|
||
def __init__(self, conn_lost): | ||
self.transport = None | ||
self.conn_lost = conn_lost | ||
self.buffered_bytes = 0 | ||
self.total_bytes = 0 | ||
|
||
def connection_made(self, tr): | ||
self.transport = tr | ||
|
||
def data_received(self, data): | ||
self.buffered_bytes += len(data) | ||
self.total_bytes += len(data) | ||
|
||
if self.transport.is_reading() and self.buffered_bytes >= TestCloseNotify.PAYLOAD_SIZE: | ||
print("app pause_reading") | ||
self.transport.pause_reading() | ||
|
||
def eof_received(self): | ||
print("app eof_received") | ||
|
||
def connection_lost(self, exc): | ||
print(f"finally received: {self.total_bytes}") | ||
self.conn_lost.set_result(None) | ||
|
||
def test_close_notify(self): | ||
|
||
conn_lost = self.loop.create_future() | ||
|
||
def server(sock): | ||
|
||
incoming = ssl.MemoryBIO() | ||
outgoing = ssl.MemoryBIO() | ||
|
||
server_context = self._create_server_ssl_context(self.ONLYCERT, self.ONLYKEY) | ||
sslobj = server_context.wrap_bio(incoming, outgoing, server_side=True) | ||
|
||
while True: | ||
try: | ||
sslobj.do_handshake() | ||
except ssl.SSLWantReadError: | ||
if outgoing.pending: | ||
sock.send(outgoing.read()) | ||
incoming.write(sock.recv(16384)) | ||
else: | ||
if outgoing.pending: | ||
sock.send(outgoing.read()) | ||
break | ||
|
||
# first send: 1024 * 50 bytes | ||
sslobj.write(self.HELLO_MSG) | ||
sock.send(outgoing.read()) | ||
|
||
time.sleep(1) | ||
|
||
# then send: 7 bytes | ||
sslobj.write(self.END_MSG) | ||
sock.send(outgoing.read()) | ||
|
||
# send close_notify but don't wait for response | ||
with self.assertRaises(ssl.SSLWantReadError): | ||
sslobj.unwrap() | ||
sock.send(outgoing.read()) | ||
|
||
sock.close() | ||
|
||
async def client(addr): | ||
cp = TestCloseNotify.ClientProto(conn_lost) | ||
client_context = self._create_client_ssl_context() | ||
tr, proto = await self.loop.create_connection(lambda: cp, *addr, ssl=client_context) | ||
|
||
# app read buffer and do some logic in 3 seconds | ||
await asyncio.sleep(3) | ||
cp.buffered_bytes = 0 | ||
# app finish operation, resume reading more from buffer | ||
tr.resume_reading() | ||
|
||
await asyncio.wait_for(conn_lost, timeout=self.TIMEOUT) | ||
await asyncio.sleep(3) | ||
tr.close() | ||
|
||
test_server = self.tcp_server(server) | ||
port = test_server._sock.getsockname()[1] | ||
thread1 = threading.Thread(target=lambda : test_server.start()) | ||
thread2 = threading.Thread(target=lambda : self.loop.run_until_complete(client(('127.0.0.1', port)))) | ||
|
||
thread1.start() | ||
thread2.start() | ||
|
||
thread1.join() | ||
thread2.join() | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correctness: The
_do_read_flush
method is called in theFLUSHING
state but doesn't handle the case when app reading is paused, which could lead to data loss when the client pauses reading.📝 Committable Code Suggestion