trinodb / trino-go-client

Go client for Trino
Apache License 2.0
135 stars 63 forks source link

Queries don't get canceled on context cancel #100

Closed sureshmula2 closed 9 months ago

sureshmula2 commented 10 months ago

Experiencing an issue where queries are not being canceled when the context is canceled. This leads to abandoned queries causing user inconvenience. Attempted resolution by setting a timeout aligned with context timeout on the connection, but encountered problems with the Trino driver. Requesting investigation and resolution to ensure proper query cancellation on context cancellation.

nineinchnick commented 10 months ago

Please provide steps to reproduce, ideally an example program

nineinchnick commented 10 months ago

Here's the relevant part of the code that should check if the context has been cancelled/closed and close the result set and cause a DELETE request to be sent to cancel the query on the Trino server:

https://github.com/trinodb/trino-go-client/blob/master/trino/trino.go#L1184

This error is generated here: https://github.com/trinodb/trino-go-client/blob/master/trino/trino.go#L861

sureshmula2 commented 10 months ago

In roundTrip function, when the context is canceled, could you provide insights what impact does it have on the connection? I think the connection is not closed and left opened. https://github.com/trinodb/trino-go-client/blob/master/trino/trino.go#L485C1-L486C25

sureshmula2 commented 9 months ago

@nineinchnick anything on this? Thanks!

nineinchnick commented 9 months ago

Can you provide reproduction steps?

sureshmula2 commented 9 months ago

While I'm trying to write down steps to reproduce this, I found an interesting thing, where I can't run two queries on a connection.

I created a connection and set the query_max_execution_time to 10 minutes. Then, I execute only one query, I don't encounter any problem. However, if I execute more than one query, it fails with the following error. I believe this should not happen.


Error:          Received unexpected error:
                            <html>
                            <head>
                            <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
                            <title>Error 500 Request failed.</title>
                            </head>
                            <body><h2>HTTP ERROR 500 Request failed.</h2>
                            <table>
                            <tr><th>URI:</th><td>/v1/statement</td></tr>
                            <tr><th>STATUS:</th><td>500</td></tr>
                            <tr><th>MESSAGE:</th><td>Request failed.</td></tr>
                            <tr><th>SERVLET:</th><td>org.glassfish.jersey.servlet.ServletContainer-6314b195</td></tr>
                            </table>

                            </body>
                            </html>

You can use following steps to reproduce the issue.

package main

import (
    "context"
    "fmt"

    "database/sql"
)

func check(ctx context.Context, config TrinoConfig) error {
    cfg, err := config.toTrinoDbConfig()
    dsn, err := cfg.FormatDSN()
    if err != nil {
        return err
    }
    db, err := sql.Open("trino", dsn)
    if err != nil {
        return err
    }

    timeoutStr := "10m"       // session timeout 10 minutes
    conn, err := db.Conn(ctx) //getting connection
    if err != nil {
        return err
    }
    _, err = conn.ExecContext(ctx, fmt.Sprintf("SET SESSION query_max_execution_time = '%s'", timeoutStr)) // setting session timeout as 10 minutes
    if err != nil {
        conn.Close()
        return err
    }

    _, err = conn.QueryContext(ctx, "select 1")
    if err != nil {
        return err
    }

    _, err = conn.QueryContext(ctx, "select 2")
    if err != nil {
        return err
    }
    return nil
}
nineinchnick commented 9 months ago

Can you report this in a separate issue? Thank you!

sureshmula2 commented 9 months ago

https://github.com/trinodb/trino-go-client/issues/104