Skip to content

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

dennissheng
Copy link

@dennissheng dennissheng commented Mar 22, 2025

I think it's not right to shutdown when eof received. It's better to wait for app resume when app reading paused. I wrote a test case(test_close_notify.py) for this situation. The server first sends 1024 * 50 bytes to client, then sends 7 bytes. The client reads the first payload(1024 * 50) then pauses reading and handles the payload in 3 seconds. After that the client resumes reading. So the client should totally receive 1024 * 50 + 7 bytes, not just 1024 * 50 bytes.

Even when there are no app reading pause, reading from buffer is needed before shutdown, cause sometimes ssl eof is in the same package of tcp fin. So when connection is closing, reading buffer is still needed to parse the EOF alert message in this package.

image

@1st1 1st1 requested a review from fantix April 16, 2025 17:20
@1st1
Copy link
Member

1st1 commented Apr 16, 2025

@fantix this is your area of expertise :)

@entelligence-ai-reviews

Walkthrough

This PR improves SSL/TLS shutdown handling in uvloop by refining the SSLProtocol state machine and introducing a new test for close_notify alert processing. The changes clarify state transitions during connection closure, add a dedicated method for read-and-shutdown logic, and enhance test coverage for SSL close_notify scenarios in asynchronous contexts. These updates aim to ensure correct resource management and protocol compliance during SSL shutdown events.

Changes

File(s) Summary
tests/test_close_notify.py Added a new test file with TestCloseNotify class to verify SSL close_notify handling and transport state management in uvloop.
uvloop/sslproto.pxd Added a new cdef method _do_read_flush(self) to the SSLProtocol class definition.
uvloop/sslproto.pyx Refined SSLProtocol state machine for shutdown: clarified WRAPPED→FLUSHING→SHUTDOWN transitions, introduced _do_read_flush() method, allowed _do_read() in FLUSHING state, and updated event loop scheduling to call _do_read_flush() in FLUSHING.

Sequence Diagram

This diagram shows the interactions between components:

sequenceDiagram
    title Connection Shutdown and Flush Operations
    
    participant Client
    participant Connection
    
    Note over Connection: The PR adds a new method: _do_read_flush()
    
    Client->>Connection: initiate shutdown
    activate Connection
    Connection->>Connection: _start_shutdown(context)
    
    alt normal operation flow
        Connection->>Connection: _do_read_into_void(context)
        Connection->>Connection: _do_flush(context)
        Connection->>Connection: _do_shutdown(context)
    else with new read flush capability
        Connection->>Connection: _do_read_into_void(context)
        Connection->>Connection: _do_read_flush()
        Note right of Connection: New method added in PR
        Connection->>Connection: _do_flush(context)
        Connection->>Connection: _do_shutdown(context)
    end
    
    Connection-->>Client: shutdown complete
    Connection->>Connection: _on_shutdown_complete(shutdown_exc)
    
    loop until shutdown completes or times out
        Connection->>Connection: _check_shutdown_timeout()
    end
    
    deactivate Connection
Loading

▶️AI Code Reviews for VS Code, Cursor, Windsurf
Install the extension

Note for Windsurf Please change the default marketplace provider to the following in the windsurf settings:

Marketplace Extension Gallery Service URL: https://marketplace.visualstudio.com/_apis/public/gallery

Marketplace Gallery Item URL: https://marketplace.visualstudio.com/items

Entelligence.ai can learn from your feedback. Simply add 👍 / 👎 emojis to teach it your preferences. More shortcuts below

Emoji Descriptions:

  • ⚠️ Potential Issue - May require further investigation.
  • 🔒 Security Vulnerability - Fix to ensure system safety.
  • 💻 Code Improvement - Suggestions to enhance code quality.
  • 🔨 Refactor Suggestion - Recommendations for restructuring code.
  • ℹ️ Others - General comments and information.

Interact with the Bot:

  • Send a message or request using the format:
    @entelligenceai + *your message*
Example: @entelligenceai Can you suggest improvements for this code?
  • Help the Bot learn by providing feedback on its responses.
    @entelligenceai + *feedback*
Example: @entelligenceai Do not comment on `save_auth` function !

Also you can trigger various commands with the bot by doing
@entelligenceai command

The current supported commands are

  1. config - shows the current config
  2. retrigger_review - retriggers the review

More commands to be added soon.

Comment on lines +602 to +605
cdef _do_read_flush(self):
self._do_read()
self._set_state(SHUTDOWN)
self._on_shutdown_complete(None)

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 the FLUSHING 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

‼️ Ensure you review the code suggestion before committing it to the branch. Make sure it replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
cdef _do_read_flush(self):
self._do_read()
self._set_state(SHUTDOWN)
self._on_shutdown_complete(None)
cdef _do_read_flush(self):
if not self._app_reading_paused:
self._do_read()
self._set_state(SHUTDOWN)
self._on_shutdown_complete(None)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants