-
Notifications
You must be signed in to change notification settings - Fork 55
Fix context loss in polling and connection close operations #295
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package context | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
| ) | ||
|
|
||
| // This implementation has been copied from https://github.com/golang/go/blob/master/src/context/context.go#L581 | ||
| // and adapted to work with this package. | ||
|
|
||
| // WithoutCancel returns a derived context that points to the parent context | ||
| // and is not canceled when parent is canceled. | ||
| // The returned context returns no Deadline or Err, and its Done channel is nil. | ||
| func WithoutCancel(parent context.Context) context.Context { | ||
| if parent == nil { | ||
| panic("cannot create context from nil parent") | ||
| } | ||
| return withoutCancelCtx{parent} | ||
| } | ||
|
|
||
| type withoutCancelCtx struct { | ||
| c context.Context | ||
| } | ||
|
|
||
| func (withoutCancelCtx) Deadline() (deadline time.Time, ok bool) { | ||
| return | ||
| } | ||
|
|
||
| func (withoutCancelCtx) Done() <-chan struct{} { | ||
| return nil | ||
| } | ||
|
|
||
| func (withoutCancelCtx) Err() error { | ||
| return nil | ||
| } | ||
|
|
||
| func (c withoutCancelCtx) Value(key any) any { | ||
| return c.c.Value(key) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,22 +67,22 @@ var _ driver.RowsColumnTypeLength = (*rows)(nil) | |
| var _ dbsqlrows.Rows = (*rows)(nil) | ||
|
|
||
| func NewRows( | ||
| connId string, | ||
| correlationId string, | ||
| ctx context.Context, | ||
| opHandle *cli_service.TOperationHandle, | ||
| client cli_service.TCLIService, | ||
| config *config.Config, | ||
| directResults *cli_service.TSparkDirectResults, | ||
| ) (driver.Rows, dbsqlerr.DBError) { | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need a check for context being nil?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't believe we need a check for |
||
| connId := driverctx.ConnIdFromContext(ctx) | ||
| correlationId := driverctx.CorrelationIdFromContext(ctx) | ||
|
|
||
| var logger *dbsqllog.DBSQLLogger | ||
| var ctx context.Context | ||
| if opHandle != nil { | ||
| logger = dbsqllog.WithContext(connId, correlationId, dbsqlclient.SprintGuid(opHandle.OperationId.GUID)) | ||
| ctx = driverctx.NewContextWithQueryId(driverctx.NewContextWithCorrelationId(driverctx.NewContextWithConnId(context.Background(), connId), correlationId), dbsqlclient.SprintGuid(opHandle.OperationId.GUID)) | ||
| ctx = driverctx.NewContextWithQueryId(ctx, dbsqlclient.SprintGuid(opHandle.OperationId.GUID)) | ||
| } else { | ||
| logger = dbsqllog.WithContext(connId, correlationId, "") | ||
| ctx = driverctx.NewContextWithCorrelationId(driverctx.NewContextWithConnId(context.Background(), connId), correlationId) | ||
| } | ||
|
|
||
| if client == nil { | ||
|
|
@@ -140,13 +140,12 @@ func NewRows( | |
| // the operations. | ||
| closedOnServer := directResults != nil && directResults.CloseOperation != nil | ||
| r.ResultPageIterator = rowscanner.NewResultPageIterator( | ||
| ctx, | ||
| d, | ||
| pageSize, | ||
| opHandle, | ||
| closedOnServer, | ||
| client, | ||
| connId, | ||
| correlationId, | ||
| r.logger(), | ||
| ) | ||
|
|
||
|
|
@@ -417,9 +416,8 @@ func (r *rows) getResultSetSchema() (*cli_service.TTableSchema, dbsqlerr.DBError | |
| req := cli_service.TGetResultSetMetadataReq{ | ||
| OperationHandle: r.opHandle, | ||
| } | ||
| ctx := driverctx.NewContextWithCorrelationId(driverctx.NewContextWithConnId(context.Background(), r.connId), r.correlationId) | ||
|
|
||
| resp, err2 := r.client.GetResultSetMetadata(ctx, &req) | ||
| resp, err2 := r.client.GetResultSetMetadata(r.ctx, &req) | ||
| if err2 != nil { | ||
| r.logger().Err(err2).Msg(err2.Error()) | ||
| return nil, dbsqlerr_int.NewRequestError(r.ctx, errRowsMetadataFetchFailed, err) | ||
|
|
||
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.
nit: We can also add usage examples from Go driver perspective, when to use this.
// WithoutCancel is used in polling operations where we need to preserve
// context values (like authentication tokens) but don't want the operation
// to be cancelled when the parent context is cancelled.