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

interop: Replace context.Background() with passed ctx #6827

Merged
merged 6 commits into from
Jan 31, 2024

Conversation

Aditya-Sood
Copy link
Contributor

Fixes #6763

RELEASE NOTES: none

Copy link

codecov bot commented Dec 1, 2023

Codecov Report

Merging #6827 (1ed10f4) into master (7935c4f) will increase coverage by 0.35%.
Report is 49 commits behind head on master.
The diff coverage is n/a.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6827      +/-   ##
==========================================
+ Coverage   83.40%   83.75%   +0.35%     
==========================================
  Files         285      287       +2     
  Lines       30879    30928      +49     
==========================================
+ Hits        25754    25904     +150     
+ Misses       4053     3963      -90     
+ Partials     1072     1061      -11     

see 65 files with indirect coverage changes

@easwars easwars requested a review from dfawley December 6, 2023 18:44
@easwars easwars added this to the 1.61 Release milestone Dec 6, 2023
@@ -267,103 +269,103 @@ func main() {
tc := testgrpc.NewTestServiceClient(conn)
switch *testCase {
case "empty_unary":
interop.DoEmptyUnaryCall(tc)
interop.DoEmptyUnaryCall(tc, ctx)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make ctx the first parameter according to common Go style.

start := time.Now()
ctx, cancel := context.WithDeadline(context.Background(), overallDeadline)
ctx, cancel := context.WithDeadline(ctx, overallDeadline)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally the caller would do this and not pass overallDeadline, and check ctx.Err() instead of time.Now.After(overallDeadline). If you don't mind making that change, too. Or we can do it separately if it turns out to be too much.

Thanks!

Copy link

This PR is labeled as requiring an update from the reporter, and no update has been received after 6 days. If no update is provided in the next 7 days, this issue will be automatically closed.

@github-actions github-actions bot added the stale label Dec 13, 2023
@github-actions github-actions bot closed this Dec 21, 2023
@Aditya-Sood
Copy link
Contributor Author

hi @dfawley, could you please reopen the PR?
I have pushed the code review changes now

Copy link
Member

@dfawley dfawley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay. Mostly LGTM; just one thing (inline).

@@ -724,9 +724,9 @@ func doOneSoakIteration(ctx context.Context, tc testgrpc.TestServiceClient, rese
// If resetChannel is false, then each RPC will be performed on tc. Otherwise, each RPC will be performed on a new
// stub that is created with the provided server address and dial options.
// TODO(mohanli-ml): Create SoakTestOptions as a parameter for this method.
func DoSoakTest(tc testgrpc.TestServiceClient, serverAddr string, dopts []grpc.DialOption, resetChannel bool, soakIterations int, maxFailures int, soakRequestSize int, soakResponseSize int, perIterationMaxAcceptableLatency time.Duration, minTimeBetweenRPCs time.Duration, overallDeadline time.Time) {
func DoSoakTest(ctx context.Context, cancel context.CancelFunc, tc testgrpc.TestServiceClient, serverAddr string, dopts []grpc.DialOption, resetChannel bool, soakIterations int, maxFailures int, soakRequestSize int, soakResponseSize int, perIterationMaxAcceptableLatency time.Duration, minTimeBetweenRPCs time.Duration) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not pass a CancelFunc. If this function needs it, it should do ctx, cancel := context.WithCancel(ctx); defer cancel(); to avoid cancelling the parent's context.

But in this case, I don't think it needs it anyway. The defer cancel() should be done in the caller, not by this function.

@Aditya-Sood
Copy link
Contributor Author

thanks @dfawley, I've pushed the change in the latest commit

one query - is the use of CancelFunc unidirectional (parent to child ctx)? or can a child be allowed to cancel the ctx for itself and its siblings?

@dfawley
Copy link
Member

dfawley commented Jan 16, 2024

one query - is the use of CancelFunc unidirectional (parent to child ctx)? or can a child be allowed to cancel the ctx for itself and its siblings?

Derived contexts cannot cancel their parents/siblings and cannot be cancelled via a method on the context itself (by design).

The thing that creates a context should be the only thing able to cancel it.

@@ -358,10 +358,12 @@ func main() {
interop.DoPickFirstUnary(ctx, tc)
logger.Infoln("PickFirstUnary done")
case "rpc_soak":
interop.DoSoakTest(ctxWithDeadline, cancel, tc, serverAddr, opts, false /* resetChannel */, *soakIterations, *soakMaxFailures, *soakRequestSize, *soakResponseSize, time.Duration(*soakPerIterationMaxAcceptableLatencyMs)*time.Millisecond, time.Duration(*soakMinTimeMsBetweenRPCs)*time.Millisecond)
interop.DoSoakTest(ctxWithDeadline, tc, serverAddr, opts, false /* resetChannel */, *soakIterations, *soakMaxFailures, *soakRequestSize, *soakResponseSize, time.Duration(*soakPerIterationMaxAcceptableLatencyMs)*time.Millisecond, time.Duration(*soakMinTimeMsBetweenRPCs)*time.Millisecond)
cancel()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cancel should be called by a defer right after the context is created (line 271).

@@ -122,7 +122,8 @@ func main() {
go func(c clientConfig) {
overallDeadline := time.Now().Add(time.Duration(*soakOverallTimeoutSeconds) * time.Second)
ctxWithDeadline, cancel := context.WithDeadline(ctx, overallDeadline)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar: it's best to defer cancel() here instead (nearly always).

The main time you wouldn't defer is if the context is created inside a loop, because you don't want to stack up a very large number of defers.

@Aditya-Sood
Copy link
Contributor Author

thank you for the clarifications @dfawley! I've pushed the changes to the branch

Copy link
Member

@dfawley dfawley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM; just a couple minor style things remain.

Comment on lines 270 to 271
overallDeadline := time.Now().Add(time.Duration(*soakOverallTimeoutSeconds) * time.Second)
ctxWithDeadline, cancel := context.WithDeadline(ctx, overallDeadline)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I missed this before: please use the more ergonomic WithTimeout instead:

ctxWithDeadline, cancel := context.WithTimeout(ctx, *soakOverallTimeoutSeconds * time.Second)

Comment on lines 392 to 393
outCtx := metadata.NewOutgoingContext(ctx, metadata.MD{"authorization": []string{kv["authorization"]}})
reply, err := tc.UnaryCall(outCtx, req)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can just shadow or replace ctx instead of making a new thing called outCtx.

// Testing with UnaryCall.
pl := ClientNewPayload(testpb.PayloadType_COMPRESSABLE, 1)
req := &testpb.SimpleRequest{
ResponseType: testpb.PayloadType_COMPRESSABLE,
ResponseSize: int32(1),
Payload: pl,
}
ctx := metadata.NewOutgoingContext(context.Background(), customMetadata)
outCtx := metadata.NewOutgoingContext(ctx, customMetadata)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above; just shadow (:=) or overwrite (=) ctx.

for i := range clients {
wg.Add(1)
go func(c clientConfig) {
interop.DoSoakTest(c.tc, c.uri, c.opts, resetChannel, *soakIterations, *soakMaxFailures, *soakRequestSize, *soakResponseSize, time.Duration(*soakPerIterationMaxAcceptableLatencyMs)*time.Millisecond, time.Duration(*soakMinTimeMsBetweenRPCs)*time.Millisecond, time.Now().Add(time.Duration(*soakOverallTimeoutSeconds)*time.Second))
overallDeadline := time.Now().Add(time.Duration(*soakOverallTimeoutSeconds) * time.Second)
ctxWithDeadline, cancel := context.WithDeadline(ctx, overallDeadline)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, please use WithTimeout instead of WithDeadline.

Copy link

This PR is labeled as requiring an update from the reporter, and no update has been received after 6 days. If no update is provided in the next 7 days, this issue will be automatically closed.

@github-actions github-actions bot added stale and removed stale labels Jan 30, 2024
@Aditya-Sood
Copy link
Contributor Author

hi @dfawley, thanks for the feedback - I'd missed the obvious use of WithTimeout instead of WithDeadline
have pushed the review changes in new commit

Copy link
Member

@dfawley dfawley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR!

@dfawley dfawley assigned arvindbr8 and unassigned dfawley Jan 31, 2024
@dfawley dfawley requested a review from arvindbr8 January 31, 2024 17:43
Copy link
Member

@arvindbr8 arvindbr8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks @Aditya-Sood

@arvindbr8 arvindbr8 merged commit a3f5ed6 into grpc:master Jan 31, 2024
14 checks passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

interop: interop functions should not call context.Background() for RPCs
5 participants