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

[ssl] Reading from non-blocking socket wrapped by SSLContext returns an empty bytes object in case if the client stays connected #125637

Open
intrelator opened this issue Oct 17, 2024 · 2 comments
Labels
stdlib Python modules in the Lib dir topic-SSL type-bug An unexpected behavior, bug, or error

Comments

@intrelator
Copy link

intrelator commented Oct 17, 2024

Bug report

Bug description:

I run my client-server application using SSLContext. And during communication I faced with situation when I receive an empty bytes object from the socket on server side. But in Wireshark dump I can see that client is present and sends data. If I add some delay after I receive empty bytes and try read from socket again I get some data. My code properly works on python 3.8 but it fails on 3.12. Also I tested without ssl on 3.12 and it works fine.

# how the socket initiated:
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain(*ssl_context)
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind((host, port))
self.socket.listen(1)
self.socket = context.wrap_socket(self.socket, server_side=True)


# how I accept client:
while not True:
    self.socket.settimeout(1)
    try:
        self.connection, _ = self.socket.accept()
        self.connection.settimeout(0.01)
        break
    except (socket.timeout, ssl.SSLError):
        continue


# how I read the data:
def read_data(self):
    data = b''
    if self.connection is not None:
        try:
            print(f"start recv at: {datetime.now().isoformat()}")
            if data := self.connection.recv(256):
                print(f"finish recv at: {datetime.now().isoformat()}")
                return data
            else:
                print(f"no data at: {datetime.now().isoformat()}")
                self.connection = None
                return b''
        except (socket.timeout, OSError):
            return data
    return data

CPython versions tested on:

3.12

Operating systems tested on:

Linux

@intrelator intrelator added the type-bug An unexpected behavior, bug, or error label Oct 17, 2024
@intrelator
Copy link
Author

Added logs
console_output.txt
image

@Zheaoli
Copy link
Contributor

Zheaoli commented Oct 17, 2024

Would you mind upload the code both client and server side? I think this would help up find the root cause more quickly

@ZeroIntensity ZeroIntensity added stdlib Python modules in the Lib dir topic-SSL labels Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir topic-SSL type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants