Skip to content

fix incorrect use of connection ctx during remote run. - #17002

Merged
mergify[bot] merged 3 commits into
matrixorigin:1.2-devfrom
m-schen:1.2-fix-remoterun-leak
Jun 19, 2024
Merged

mergify[bot] merged 3 commits into
matrixorigin:1.2-devfrom
m-schen:1.2-fix-remoterun-leak

Conversation

@m-schen

@m-schen m-schen commented Jun 19, 2024 •

Copy link
Copy Markdown
Contributor

User description

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

https://github.com/matrixorigin/MO-Cloud/issues/3542
https://github.com/matrixorigin/MO-Cloud/issues/2788
https://github.com/matrixorigin/MO-Cloud/issues/3281

What this PR does / why we need it:

fix a bug that the TCP context was incorrectly used as a STREAM context during the remote run,
this will cause an inability to properly determine whether the stream had been disconnected.


PR Type

Bug fix


Description

  • Added SessionCtx method to clientSession and ClientSession interface to retrieve the session context.
  • Replaced incorrect usage of ctx with connectionCtx and messageCtx in remoterunServer.go.
  • Updated messageReceiverOnServer struct and related functions to properly handle connection and message contexts.

Changes walkthrough 📝

Relevant files
Enhancement
server.go
Add method to retrieve session context in client session 

pkg/common/morpc/server.go

  • Added SessionCtx method to clientSession to return the session
    context.
  • +4/-0     
    types.go
    Add session context retrieval method to ClientSession interface

    pkg/common/morpc/types.go

    • Added SessionCtx method to ClientSession interface.
    +2/-0     
    Bug fix
    remoterunServer.go
    Fix incorrect context usage in remote run server                 

    pkg/sql/compile/remoterunServer.go

  • Replaced usage of ctx with connectionCtx and messageCtx in various
    functions.
  • Updated messageReceiverOnServer struct to include connectionCtx and
    messageCtx.
  • Modified newMessageReceiverOnServer to initialize connectionCtx using
    SessionCtx.
  • Updated error handling and message sending functions to use
    messageCtx.
  • +21/-15 

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    @qodo-code-review

    Copy link
    Copy Markdown

    PR-Agent was enabled for this repository. To continue using it, please link your git user with your CodiumAI identity here.

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review [1-5] 3
    🧪 Relevant tests No
    🔒 Security concerns No
    ⚡ Key issues to review Context Handling:
    The changes in context handling from a single context to separate messageCtx and connectionCtx are significant. Reviewers should ensure that the lifecycle and cancellation of these contexts are handled correctly across different parts of the application to avoid memory leaks or deadlocks.
    Error Handling:
    The modification of error messages to use messageCtx instead of ctx needs careful review to ensure that the context used will still be active when errors are processed, thus avoiding any potential issues with using an expired context.

    @mergify
    mergify Bot requested a review from sukki37 June 19, 2024 09:23
    @matrix-meow matrix-meow added the size/S Denotes a PR that changes [10,99] lines label Jun 19, 2024
    @mergify mergify Bot added the kind/bug Something isn't working label Jun 19, 2024
    @qodo-code-review

    Copy link
    Copy Markdown

    PR-Agent was enabled for this repository. To continue using it, please link your git user with your CodiumAI identity here.

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Add a nil check for receiver.connectionCtx before using it in select statements to prevent potential runtime panics

    To ensure that the receiver.connectionCtx is not nil when accessed, add a nil check before
    using it in the select statements. This will prevent potential runtime panics if
    connectionCtx is unexpectedly nil.

    pkg/sql/compile/remoterunServer.go [148]

    -case <-receiver.connectionCtx.Done():
    +case <-func() <-chan struct{} {
    +    if receiver.connectionCtx != nil {
    +        return receiver.connectionCtx.Done()
    +    }
    +    return nil
    +}():
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding a nil check is crucial to prevent runtime panics due to a nil context, which is a significant issue in concurrent Go applications.

    8
    Possible issue
    Add a check to ensure receiver.messageCtx is not nil before using it in error handling to prevent potential nil pointer dereferences

    To ensure that the error message context is always available, add a check to verify that
    receiver.messageCtx is not nil before using it in error handling.

    pkg/sql/compile/remoterunServer.go [542]

    -return nil, moerr.NewInternalError(receiver.messageCtx, "get dispatch process by uuid timeout")
    +if receiver.messageCtx != nil {
    +    return nil, moerr.NewInternalError(receiver.messageCtx, "get dispatch process by uuid timeout")
    +}
    +return nil, moerr.NewInternalError(context.Background(), "get dispatch process by uuid timeout")
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: This suggestion correctly addresses a potential nil pointer dereference, which is a critical issue that could crash the application at runtime.

    8
    Best practice
    Add a deferred call to the cancel function when creating a new context with context.WithCancel to avoid potential memory leaks

    When creating a new context with context.WithCancel, ensure that the cancel function is
    called to avoid potential memory leaks. Add a deferred call to runningCancel after
    creating the context.

    pkg/sql/compile/remoterunServer.go [364]

     runningCtx, runningCancel := context.WithCancel(receiver.connectionCtx)
    +defer runningCancel()
     
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    Why: Ensuring that the cancel function is called is a best practice to avoid memory leaks in Go, especially important in long-running applications.

    6
    Enhancement
    Rename connectionCtx to sessionCtx for better readability and maintainability

    To improve readability and maintainability, consider renaming connectionCtx to sessionCtx
    to better reflect its purpose as the session context.

    pkg/sql/compile/remoterunServer.go [267]

    -connectionCtx context.Context
    +sessionCtx context.Context
     
    • Apply this suggestion
    Suggestion importance[1-10]: 3

    Why: While renaming might improve readability, it's a minor enhancement and does not impact functionality or performance.

    3

    @matrix-meow matrix-meow added size/M Denotes a PR that changes [100,499] lines and removed size/S Denotes a PR that changes [10,99] lines labels Jun 19, 2024
    @mergify
    mergify Bot merged commit 4bf3fe7 into matrixorigin:1.2-dev Jun 19, 2024
    @m-schen
    m-schen deleted the 1.2-fix-remoterun-leak branch June 28, 2024 02:55
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Labels

    Bug fix kind/bug Something isn't working Review effort [1-5]: 3 size/M Denotes a PR that changes [100,499] lines

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    7 participants