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

pubsub: Receive fails to hide context.Canceled error when using synchronous pull #3746

Closed
mgabeler-lee-6rs opened this issue Feb 26, 2021 · 1 comment · Fixed by #3752
Closed
Assignees
Labels
api: pubsub Issues related to the Pub/Sub API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@mgabeler-lee-6rs
Copy link

Client

PubSub

Environment

Various

Go Environment

$ go version
go version go1.16 linux/amd64

$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/mgl/.cache/go-build"
GOENV="/home/mgl/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/mgl/src/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/mgl/src/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.16"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.16/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build4089208195=/tmp/go-build -gno-record-gcc-switches"

Code

func foo(client *pubsub.Client) {
	sub := client.Subscription("foo")
	// enabling synchronous receive causes Receive to return a context cancelled error instead of nil
	sub.ReceiveSettings.Synchronous = true
	ctx, cancel := context.WithCancel(context.Background())
	err := sub.Receive(ctx, func(c context.Context, m *pubsub.Message) {
		m.Ack()
		cancel()
	})
	if err != nil {
		panic(err)
	}
}

Expected behavior

The call to sub.Receive should return a nil error when it is terminated through context cancellation.

Actual behavior

The call to sub.Receive returns a version of context.Canceled, wrapped/converted to a gRPC status error, if and only if Synchronous pull is enabled.

Additional context

I've traced this so far as this error handling logic in messageIterator.pullMessages:

switch {
case err == context.Canceled:
return nil, nil
case err != nil:
return nil, err
default:
return res.ReceivedMessages, nil
}

In this case, it's trying to replace the context cancellation error with nil, but that cancellation error has already been wrapped into a gRPC one at this point ... which isn't surprising, because it came from a gRPC client call :)

I believe this can be fixed by updating the switch thus:

	switch {
	case err == context.Canceled:
		return nil, nil
	case status.Code(err) == codes.Canceled: // ⇐ add this case
		return nil, nil
	case err != nil:
		return nil, err
	default:
		return res.ReceivedMessages, nil
	}
@mgabeler-lee-6rs mgabeler-lee-6rs added the triage me I really want to be triaged. label Feb 26, 2021
@product-auto-label product-auto-label bot added the api: pubsub Issues related to the Pub/Sub API. label Feb 26, 2021
@meredithslota meredithslota added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. and removed triage me I really want to be triaged. labels Feb 26, 2021
@hongalex
Copy link
Member

Totally agreed, thanks for opening this issue and including your investigations! I opened a PR without your suggested change to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: pubsub Issues related to the Pub/Sub API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants