Skip to content

Commit

Permalink
fix: fix deadlock issue when creating child actor (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tochemey authored Jun 30, 2024
1 parent 28d2fa9 commit c31993b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
13 changes: 8 additions & 5 deletions actors/pid.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,7 @@ func (x *pid) SpawnChild(ctx context.Context, name string, actor Actor) (PID, er
return cid, nil
}

x.rwLocker.Lock()
defer x.rwLocker.Unlock()
x.rwLocker.RLock()

// create the child actor options child inherit parent's options
opts := []pidOption{
Expand Down Expand Up @@ -585,16 +584,20 @@ func (x *pid) SpawnChild(ctx context.Context, name string, actor Actor) (PID, er
if err != nil {
span.SetStatus(codes.Error, "SpawnChild")
span.RecordError(err)
x.rwLocker.RUnlock()
return nil, err
}

x.children.set(cid)
x.Watch(cid)
eventsStream := x.eventsStream

x.rwLocker.RUnlock()
span.SetStatus(codes.Ok, "SpawnChild")

if x.eventsStream != nil {
x.eventsStream.Publish(eventsTopic, &goaktpb.ActorChildCreated{
x.Watch(cid)

if eventsStream != nil {
eventsStream.Publish(eventsTopic, &goaktpb.ActorChildCreated{
Address: cid.ActorPath().RemoteAddress(),
CreatedAt: timestamppb.Now(),
Parent: x.ActorPath().RemoteAddress(),
Expand Down
53 changes: 53 additions & 0 deletions actors/pid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"google.golang.org/protobuf/proto"

"github.com/tochemey/goakt/v2/goaktpb"
"github.com/tochemey/goakt/v2/internal/eventstream"
"github.com/tochemey/goakt/v2/log"
"github.com/tochemey/goakt/v2/telemetry"
"github.com/tochemey/goakt/v2/test/data/testpb"
Expand Down Expand Up @@ -1024,6 +1025,58 @@ func TestSpawnChild(t *testing.T) {
err = parent.Shutdown(ctx)
assert.NoError(t, err)
})
t.Run("With child created event published", func(t *testing.T) {
// create a test context
ctx := context.TODO()
// create the actor path
actorPath := NewPath("Parent", NewAddress("sys", "host", 1))

eventsStream := eventstream.New()

// add a subscriber
subsriber := eventsStream.AddSubscriber()
eventsStream.Subscribe(subsriber, eventsTopic)

// create the parent actor
parent, err := newPID(ctx, actorPath,
newSupervisor(),
withInitMaxRetries(1),
withCustomLogger(log.DiscardLogger),
withEventsStream(eventsStream),
withAskTimeout(replyTimeout))

require.NoError(t, err)
assert.NotNil(t, parent)

// create the child actor
child, err := parent.SpawnChild(ctx, "SpawnChild", newSupervised())
assert.NoError(t, err)
assert.NotNil(t, child)

assert.Len(t, parent.Children(), 1)

time.Sleep(time.Second)

var events []*goaktpb.ActorChildCreated
for message := range subsriber.Iterator() {
// get the event payload
payload := message.Payload()
switch msg := payload.(type) {
case *goaktpb.ActorChildCreated:
events = append(events, msg)
}
}

require.NotEmpty(t, events)
require.Len(t, events, 1)

event := events[0]
assert.True(t, proto.Equal(parent.ActorPath().RemoteAddress(), event.GetParent()))

//stop the actor
err = parent.Shutdown(ctx)
assert.NoError(t, err)
})
}
func TestPoisonPill(t *testing.T) {
ctx := context.TODO()
Expand Down

0 comments on commit c31993b

Please sign in to comment.