aws-sdk-go-v2: DynamoDB returns operation error `use of closed network`

Describe the bug

SDK often returns the error below after updated SDK version.

acutal error:

operation error DynamoDB: Query, $tableName: https response error StatusCode: 200, RequestID: 123456, deserialization failed, failed to decode response body, read tcp $src->$dst: use of closed network connection

Expected Behavior

No errors

Current Behavior

operation error DynamoDB: Query, $tableName: https response error StatusCode: 200, RequestID: 123456, deserialization failed, failed to decode response body, read tcp $src->$dst: use of closed network connection

Reproduction Steps

	ddb := dynamodb.NewFromConfig(defaultCfg)
	result, err := ddb.Query(
		ctx,
		&dynamodb.QueryInput{
			TableName:                 aws.String(tableName),
			ExpressionAttributeNames:  expr.Names(),
			ExpressionAttributeValues: expr.Values(),
			KeyConditionExpression:    expr.KeyCondition(),
		},
	)

Possible Solution

No response

Additional Information/Context

No response

AWS Go SDK V2 Module Versions Used

	github.com/aws/aws-sdk-go-v2 v1.16.8
	github.com/aws/aws-sdk-go-v2/config v1.15.15
	github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.9.8
	github.com/aws/aws-sdk-go-v2/feature/dynamodb/expression v1.4.14
	github.com/aws/aws-sdk-go-v2/service/dynamodb v1.15.10
	github.com/aws/smithy-go v1.12.0

Compiler and Version used

1.19

Operating System and version

Linux

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 33 (10 by maintainers)

Commits related to this issue

Most upvoted comments

We are seeing the same issue as well, can we please resurrect this issue?

We also are having significant developer pain when running tests with Github Actions. Once we reach a critical mass of tests, they begin to fail consistently not intermittently. As others have suggested, we tried forking the repo and commenting out the suggested block of code. I also concluded (as did @isaiahvita) that the commented-out block should not have an impact on this issue. But with nothing else to go off, it was worth a try. And removing removing the block of code did stop failures (at least until we built up another critical mass of tests-- so for a month or so, a beautiful, consistent month).

I spent more time attempting to debug the issue in smithy itself as I had a path to reproduce it 100% of the time and quickly learned that any extra infrastructure I added to help debug (with or without the commented block), tended to “fix” the issue.

To me this seems to indicate there is a real race condition somewhere in the code and that simply adding or subtracting work seems to shuffle load just enough that the is less likely to occur. The fact that the issue only occurs after upgrading to sdk2 should also be a clear sign of this. Alas, I did not have time to continue my investigation and now the error is back.

Please re-open the issue.

As far as the error being returned is concerned - the SDK is behaving correctly by not retrying in this case. As people have noted in this thread and others, we DO NOT retry this class of error (ECONNRESET) because it is not deterministically safe to do so.

As for the root case, we have no hand in the networking internals of SDK requests - we defer to the configured HTTP client, which is either the standard library implementation in the default case, or one provided by the user.

To those affected by this issue, 2 things–

retrying these errors

You can make the underlying error net.ErrClosed retryable in your client with the following:

type retryErrClosed struct {
	aws.Retryer
}

func (r *retryErrClosed) IsErrorRetryable(err error) bool {
	return errors.Is(err, net.ErrClosed) || r.Retryer.IsErrorRetryable(err)
}
	
svc := s3.NewFromConfig(cfg, func(o *s3.Options) {
	o.Retryer = &retryErrClosed{o.Retryer}	
})

This should be safe to do for get-type operations such as dynamodb.Query.

root-causing

As of this writing we have been unable to reproduce this in-house whatsoever. I’m going to ask that anyone affected by this issue implement some basic net.Conn tracing to help grant more visibility into where the underlying Close() calls are coming from. The following DialContext implementation, when provided to a transport, will save the stack trace on a Close() call, and dump it to stdout when a Read() on the underlying conn catches this.

If you are affected by this issue and are in a position to do so, please implement this tracing logic in your clients and share any of the stack traces produced from it:

import (
	awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http"
)

type dialContext func(ctx context.Context, network, addr string) (net.Conn, error)

func traceConn(dc dialContext) dialContext {
	return func(ctx context.Context, network, addr string) (net.Conn, error) {
		conn, err := dc(ctx, network, addr)
		return &tracedConn{Conn: conn}, err
	}
}

type tracedConn struct {
	net.Conn
	closeTrace string
}

func (c *tracedConn) Read(p []byte) (int, error) {
	n, err := c.Conn.Read(p)
	if len(c.closeTrace) > 0 {
		fmt.Println()
		fmt.Println("!!! READ ON CLIENT-CLOSED CONN !!!")
		fmt.Println(c.closeTrace)
		fmt.Println("!!! -(end trace)-------------- !!!")
		fmt.Println()
	}
	return n, err
}

func (c *tracedConn) Close() error {
	c.closeTrace = string(debug.Stack())
	return c.Conn.Close()
}

func main() {
	// ...

	svc := s3.NewFromConfig(cfg, func(o *s3.Options) {
		client := o.HTTPClient.(*awshttp.BuildableClient)
		o.HTTPClient = client.WithTransportOptions(func(t *http.Transport) {
			t.DialContext = traceConn(t.DialContext)
		})
	})

	// ...
}

Hi everyone,

Since more and more people are reporting being impacted I have re-opened this issue.

In our previous reproduction attempts we were not able to recreate the reported behavior. The next step is to ask one of the customers here on the thread to provide a minimal github repository with code that can reliably reproduce the behavior (even if it means seeing intermittent errors reliably).

That would give us a little more to go off of.

Thanks again, Ran~

We probably have same problem on SQS DeleteMessage. (Request ID and source IP are masked)

operation error SQS: DeleteMessage, https response error StatusCode: 200, RequestID: xxxx-xxxx-xxxx-xxxx-xxxx, deserialization failed, failed to discard response body, read tcp xxx.xxx.xxx.xxx:xxx->18.183.37.45:443: use of closed network connection

go 1.19.3, aws-sdk-go-v2 v1.17.2

Just leaving a comment here to confirm that this problem still exists with the following versions and a local running dynamodb docker instance in gitlab:

Go v1.20.3 github.com/aws/aws-sdk-go-v2 v1.17.7 github.com/aws/aws-sdk-go-v2/config v1.18.2 github.com/aws/aws-sdk-go-v2/credentials v1.13.2 github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.10.19 github.com/aws/aws-sdk-go-v2/service/dynamodb v1.19.2 github.com/aws/smithy-go v1.13.5 amazon/dynamodb-local:1.21.0

A retry strategy cannot always be used to retry this error, due to the same reason you are not adding this as a retryable error on SDK level: e.g. adding an element to a list in dynamodb with an update expression is not idempotent and cannot be retried since we do not know if the request was successful or not.

Currently our BDD test suite failes about ~5% of times in the gitlab pipeline because of this error, which is mildly annoying. If our tests are failing then it’s usually in about the same scenario and steps, so I could assume it could be related to a lifecycle of a connection. Either from client or from local dynamodb perspective.

To me it seems that this is happening when the running system is under heavy load.

you can fork smithy-go with a small fix that outcomments a three-line block to avoid closing the request body too early, just as @psmarcin did, and we are also doing now for our production services now - with no problems at all.

https://github.com/psmarcin/smithy-go https://github.com/cwd-nial/smithy-go

Maybe as @a-h commented, does the issue just happen when connecting to DynamoDB local running in Docker?