Skip to content

Commit dbaf227

Browse files
authored
fix(state): CoreAccessor manages its own context (#1247)
Resolves #1248 and #1246
1 parent f1c25e4 commit dbaf227

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

Diff for: nodebuilder/node_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,14 @@ func TestLifecycle(t *testing.T) {
3636
err := node.Start(ctx)
3737
require.NoError(t, err)
3838

39+
// ensure the state service is running
40+
require.False(t, node.StateServ.IsStopped())
41+
3942
err = node.Stop(ctx)
4043
require.NoError(t, err)
44+
45+
// ensure the state service is stopped
46+
require.True(t, node.StateServ.IsStopped())
4147
})
4248
}
4349
}

Diff for: state/core_access.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,11 @@ func NewCoreAccessor(
7373
}
7474

7575
func (ca *CoreAccessor) Start(ctx context.Context) error {
76-
ca.ctx, ca.cancel = context.WithCancel(ctx)
7776
if ca.coreConn != nil {
7877
return fmt.Errorf("core-access: already connected to core endpoint")
7978
}
79+
ca.ctx, ca.cancel = context.WithCancel(context.Background())
80+
8081
// dial given celestia-core endpoint
8182
endpoint := fmt.Sprintf("%s:%s", ca.coreIP, ca.grpcPort)
8283
client, err := grpc.DialContext(ctx, endpoint, grpc.WithTransportCredentials(insecure.NewCredentials()))
@@ -100,21 +101,32 @@ func (ca *CoreAccessor) Start(ctx context.Context) error {
100101
}
101102

102103
func (ca *CoreAccessor) Stop(context.Context) error {
103-
defer ca.cancel()
104+
if ca.cancel == nil {
105+
log.Warn("core accessor already stopped")
106+
return nil
107+
}
104108
if ca.coreConn == nil {
105109
log.Warn("no connection found to close")
106110
return nil
107111
}
112+
defer ca.cancelCtx()
113+
108114
// close out core connection
109115
err := ca.coreConn.Close()
110116
if err != nil {
111117
return err
112118
}
119+
113120
ca.coreConn = nil
114121
ca.queryCli = nil
115122
return nil
116123
}
117124

125+
func (ca *CoreAccessor) cancelCtx() {
126+
ca.cancel()
127+
ca.cancel = nil
128+
}
129+
118130
func (ca *CoreAccessor) constructSignedTx(
119131
ctx context.Context,
120132
msg sdktypes.Msg,

Diff for: state/core_access_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package state
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestLifecycle(t *testing.T) {
11+
ca := NewCoreAccessor(nil, nil, "", "", "")
12+
ctx, cancel := context.WithCancel(context.Background())
13+
// start the accessor
14+
err := ca.Start(ctx)
15+
require.NoError(t, err)
16+
// ensure accessor isn't stopped
17+
require.False(t, ca.IsStopped())
18+
// cancel the top level context (this should not affect the lifecycle of the
19+
// accessor as it should manage its own internal context)
20+
cancel()
21+
// ensure accessor was unaffected by top-level context cancellation
22+
require.False(t, ca.IsStopped())
23+
// stop the accessor
24+
stopCtx, stopCancel := context.WithCancel(context.Background())
25+
t.Cleanup(stopCancel)
26+
err = ca.Stop(stopCtx)
27+
require.NoError(t, err)
28+
// ensure accessor is stopped
29+
require.True(t, ca.IsStopped())
30+
// ensure that stopping the accessor again does not return an error
31+
err = ca.Stop(stopCtx)
32+
require.NoError(t, err)
33+
}

0 commit comments

Comments
 (0)