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

Ensure that calling grpcServer.Stop() only once #412

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

hunter007
Copy link
Contributor

@hunter007 hunter007 commented Jun 5, 2023

Fix #411

@hunter007 hunter007 marked this pull request as ready for review June 5, 2023 08:09
@hunter007 hunter007 requested a review from a team as a code owner June 5, 2023 08:09
@codecov
Copy link

codecov bot commented Jun 5, 2023

Codecov Report

Merging #412 (ae4c514) into main (df5bd08) will increase coverage by 0.39%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #412      +/-   ##
==========================================
+ Coverage   69.97%   70.37%   +0.39%     
==========================================
  Files          33       33              
  Lines        2651     2653       +2     
==========================================
+ Hits         1855     1867      +12     
+ Misses        695      686       -9     
+ Partials      101      100       -1     
Impacted Files Coverage Δ
service/grpc/service.go 77.96% <100.00%> (+18.31%) ⬆️

Signed-off-by: hunter007 <[email protected]>
return s.grpcServer.Serve(s.listener)
}

// Stop stops the previously-started service.
func (s *Server) Stop() error {
if atomic.LoadUint32(&s.started) == 0 {
Copy link
Member

Choose a reason for hiding this comment

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

If not started, double call will return nil. Is it an issue that we miss update the start status?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I will correct it

Copy link
Member

Choose a reason for hiding this comment

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

Also, we can use start status to check stop, no need to add a new one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can prevent restart and re-stop using stopped.

If no stopped, when started = 0, we can call Start() which set started=1.

But, call Stop(), if set started = 0, we can restart server? if keep started = 1, we can re-stop?

Copy link
Member

@daixiang0 daixiang0 Jun 12, 2023

Choose a reason for hiding this comment

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

When started is:

  • true, can stop, start/re-start but do nothing
  • false, can start, stop/re-stop but do nothing

Only real stop/start will update started.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I write a test:

func TestReStopServer(t *testing.T) {
	server := getTestServer()
	startTestServer(server)
	time.Sleep(time.Second)

	assert.NotNil(t, server)
	err := server.Stop()
	assert.Nilf(t, err, "error stopping server")

	fmt.Print("Re-stop server\n")

	err = server.Stop()
	assert.Nilf(t, err, "error re-stopping server")
}

the test panic:

go test -timeout 30s -run ^TestReStopServer$ github.com/dapr/go-sdk/service/grpc -v

=== RUN   TestReStopServer
Re-stop server
--- FAIL: TestReStopServer (1.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x180 pc=0x10475103c]

goroutine 34 [running]:
testing.tRunner.func1.2({0x1048f8860, 0x104d0c280})
	/go1.20/src/testing/testing.go:1526 +0x1c8
testing.tRunner.func1()
	/go1.20/src/testing/testing.go:1529 +0x364
panic({0x1048f8860, 0x104d0c280})
	/go1.20/src/runtime/panic.go:884 +0x1f4
google.golang.org/grpc.(*Server).Stop(0x0)
	/gopkg/mod/google.golang.org/[email protected]/server.go:1800 +0x2c
github.com/dapr/go-sdk/service/grpc.(*Server).Stop(...)
	/dapr_repos/go-sdk/service/grpc/service.go:112
github.com/dapr/go-sdk/service/grpc.TestReStopServer(0x0?)
	/dapr_repos/go-sdk/service/grpc/service_test.go:79 +0x1c4
testing.tRunner(0x140001171e0, 0x10499b2f0)
	/go1.20/src/testing/testing.go:1576 +0x104
created by testing.(*T).Run
	/go1.20/src/testing/testing.go:1629 +0x370

I made this PR to resolve this issue: #411

Signed-off-by: hunter007 <[email protected]>
@hunter007 hunter007 requested a review from daixiang0 June 6, 2023 10:39
Copy link
Member

Choose a reason for hiding this comment

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

I think we should be using the require package for error evaluations e.g. require.Error / require.NoError

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Panic when call server.Stop() twice
3 participants