Skip to content

Client timeout of 0 causes immediate "context deadline exceeded" #126

Closed

Description

@timbunce

This is arguably a duplicate of #14 which pointed out a doc deficiency and was addressed by telling the user to use operations.New*Params() functions.

It's also arguably a duplicate of go-swagger/go-swagger#919 where somebody else stumbled into the same problem. @casualjim responded that "We need a way to express forever because there are certain cases where you really don't want a request to timeout.", "... you want the connection to be opened forever, forever is signaled by using 0" and the case was closed.

Unless I'm misunderstanding something that seems to be a mistake as the code doesn't operate that way.

I think the hasTimeout var should only be set true if request.timeout is non-zero.

Activity

  1. said-saifi commented on Jun 1, 2021

    @said-saifi

    Does the suggested solution have any drawbacks? As the current code also causes an issue where the timeout is not respected. So if you have both context set and timeout parameter set then the timeout will just be ignored. For example:

    Issue

    // let's assume that we have a ctx defined before that did not have a deadline
    
    params := NewAddOneParamsWithContext(ctx)
    params.SetTimeout(10*time.Second)
    
    // calling AddOne which will then call Submit function
    c.AddOne(params, ...) 

    The existing code will not respect the timeout as the context is set.

    Workaround

    A workaround is to pass the timeout in the context, so you can instead do:

    // let's assume that we have a ctx defined before that did not have a deadline
    
    ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
    defer cancel()
    
    params := NewAddOneParamsWithContext(ctx)
    
    // calling AddOne which will then call Submit function
    c.AddOne(params, ...) 

    Possible solution 1:

    If we want to always prioritize request.timeout even if context has already a deadline. (May not be a good idea as we will not respect the existing context deadline).

    var pctx context.Context
    switch {
    case operation.Context != nil:
        pctx = operation.Context
    case r.Context != nil:
        pctx = r.Context
    default:
        pctx = context.Background()
    }
    
    var ctx context.Context
    var cancel context.CancelFunc
    if request.timeout != 0 {
        ctx, cancel = context.WithTimeout(pctx, request.timeout)
    } else {
        ctx, cancel = context.WithCancel(pctx)
    }
    defer cancel()

    Possible solution 2:

    Only assuming timeout exists if context has a deadline and that's less than the request timeout. Any drawbacks for this solution?

    var pctx context.Context
    switch {
    case operation.Context != nil:
    	pctx = operation.Context
    case r.Context != nil:
    	pctx = r.Context
    default:
    	pctx = context.Background()
    }
    
    var ctx context.Context
    var cancel context.CancelFunc
    
    if request.timeout != 0 {
    	ctxDeadline, hasDeadline := pctx.Deadline()
    	requestDeadline := time.Now().Add(request.timeout)
    	
    	if hasDeadline && ctxDeadline.Before(requestDeadline) {
    		ctx, cancel = context.WithCancel(pctx)
    	} else {
    		ctx, cancel = context.WithTimeout(pctx, request.timeout)
    	}
    
    } else {
    	ctx, cancel = context.WithCancel(pctx)
    }
    
    defer cancel()
          
  2. youyuanwu commented on Jun 5, 2021

    @youyuanwu
    Member

    "Possible solution 1" above is ok. "Possible solution 2" is not standard of how to handle ctx in Go.

  3. added a commit that references this issue on Dec 13, 2023
    b81b10f
  4. added a commit that references this issue on Dec 14, 2023
    d337653
  5. added a commit that references this issue on Dec 18, 2023
    552b300
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions