@@ -4083,19 +4083,8 @@ static term nif_erlang_group_leader(Context *ctx, int argc, term argv[])
4083
4083
VALIDATE_VALUE (leader , term_is_pid );
4084
4084
4085
4085
int local_process_id = term_to_local_process_id (pid );
4086
- Context * target = globalcontext_get_process_lock (ctx -> global , local_process_id );
4087
- if (IS_NULL_PTR (target )) {
4088
- RAISE_ERROR (BADARG_ATOM );
4089
- }
4090
-
4091
- if (target != ctx ) {
4092
- // always use signals when changing group leader of another context
4093
- // this will avoid any heap access during GC and in general avoids a number
4094
- // of annoying situations
4095
- // this will be async
4096
- mailbox_send_term_signal (target , SetGroupLeaderSignal , leader );
4097
- } else {
4098
- // this will be sync
4086
+ if (ctx -> process_id == local_process_id ) {
4087
+ // use a synchronous approach when updating group leader for this process
4099
4088
if (term_is_local_pid (leader )) {
4100
4089
ctx -> group_leader = leader ;
4101
4090
} else {
@@ -4107,9 +4096,20 @@ static term nif_erlang_group_leader(Context *ctx, int argc, term argv[])
4107
4096
}
4108
4097
ctx -> group_leader = memory_copy_term_tree (& ctx -> heap , leader );
4109
4098
}
4110
- }
4099
+ } else {
4100
+ Context * target = globalcontext_get_process_lock (ctx -> global , local_process_id );
4101
+ if (IS_NULL_PTR (target )) {
4102
+ RAISE_ERROR (BADARG_ATOM );
4103
+ }
4111
4104
4112
- globalcontext_get_process_unlock (ctx -> global , target );
4105
+ // always use signals when changing group leader of another context
4106
+ // this will avoid any heap access during GC and in general avoids a number
4107
+ // of annoying situations
4108
+ // this will be async
4109
+ mailbox_send_term_signal (target , SetGroupLeaderSignal , leader );
4110
+
4111
+ globalcontext_get_process_unlock (ctx -> global , target );
4112
+ }
4113
4113
return TRUE_ATOM ;
4114
4114
}
4115
4115
}
0 commit comments