Skip to content

Commit

Permalink
OS-5323 Java Processes that fork exit intermittently
Browse files Browse the repository at this point in the history
Reviewed by: Patrick Mooney <patrick.mooney@joyent.com>
  • Loading branch information
jjelinek committed Apr 14, 2016
1 parent a4fcf8b commit 4af82cf
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
23 changes: 7 additions & 16 deletions usr/src/lib/brand/lx/lx_brand/common/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,28 +101,20 @@ struct clone_state {
lx_tsd_t *c_lx_tsd; /* tsd area for thread */
};

/*
* Counter incremented when we vfork(2) ourselves, and decremented when the
* vfork(2)ed child exit(2)s or exec(2)s.
*/
int lx_is_vforked = 0;

long
lx_exit(uintptr_t p1)
{
int status = (int)p1;
lx_tsd_t *lx_tsd;
lx_tsd_t *lx_tsd = lx_get_tsd();

/*
* If we are a vfork(2)ed child, we need to exit as quickly and
* cleanly as possible to avoid corrupting our parent.
*/
if (lx_is_vforked != 0) {
if (lx_tsd->lxtsd_is_vforked != 0) {
_exit(status);
}

lx_tsd = lx_get_tsd();

lx_tsd->lxtsd_exit = LX_ET_EXIT;
lx_tsd->lxtsd_exit_status = status;

Expand All @@ -148,18 +140,16 @@ long
lx_group_exit(uintptr_t p1)
{
int status = (int)p1;
lx_tsd_t *lx_tsd;
lx_tsd_t *lx_tsd = lx_get_tsd();

/*
* If we are a vfork(2)ed child, we need to exit as quickly and
* cleanly as possible to avoid corrupting our parent.
*/
if (lx_is_vforked != 0) {
if (lx_tsd->lxtsd_is_vforked != 0) {
_exit(status);
}

lx_tsd = lx_get_tsd();

lx_tsd->lxtsd_exit = LX_ET_EXIT_GROUP;
lx_tsd->lxtsd_exit_status = status;

Expand Down Expand Up @@ -327,6 +317,7 @@ lx_clone(uintptr_t p1, uintptr_t p2, uintptr_t p3, uintptr_t p4, uintptr_t p5)
int fork_flags = 0;
int ptrace_event;
int error = 0;
lx_tsd_t *lx_tsd = lx_get_tsd();

if (flags & LX_CLONE_SETTLS) {
lx_debug("lx_clone(flags=0x%x stk=0x%p ptidp=0x%p ldt=0x%p "
Expand Down Expand Up @@ -419,10 +410,10 @@ lx_clone(uintptr_t p1, uintptr_t p2, uintptr_t p3, uintptr_t p4, uintptr_t p5)
* concurrently with our child.
*/
lx_sighandlers_save(&saved);
lx_is_vforked++;
lx_tsd->lxtsd_is_vforked++;
rval = vforkx(fork_flags);
if (rval != 0) {
lx_is_vforked--;
lx_tsd->lxtsd_is_vforked--;
lx_sighandlers_restore(&saved);
}
} else {
Expand Down
7 changes: 4 additions & 3 deletions usr/src/lib/brand/lx/lx_brand/common/fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2015 Joyent, Inc. All rights reserved.
* Copyright 2016 Joyent, Inc. All rights reserved.
*/

#include <errno.h>
Expand Down Expand Up @@ -107,6 +107,7 @@ lx_vfork(void)
lx_sighandlers_t saved;
ucontext_t vforkuc;
ucontext_t *ucp;
lx_tsd_t *lx_tsd = lx_get_tsd();

ucp = lx_syscall_regs();

Expand All @@ -126,11 +127,11 @@ lx_vfork(void)
_sigoff();
lx_stack_prefork();
lx_sighandlers_save(&saved);
lx_is_vforked++;
lx_tsd->lxtsd_is_vforked++;
ret = vfork();
if (ret != 0) {
/* parent/error */
lx_is_vforked--;
lx_tsd->lxtsd_is_vforked--;
lx_sighandlers_restore(&saved);
}

Expand Down
4 changes: 3 additions & 1 deletion usr/src/lib/brand/lx/lx_brand/sys/lx_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2015 Joyent, Inc
* Copyright 2016 Joyent, Inc
*/

#ifndef _SYS_LX_THREAD_H
Expand All @@ -42,6 +42,8 @@ typedef enum lx_exit_type {
} lx_exit_type_t;

typedef struct lx_tsd {
/* per-thread flag set on parent vfork, cleared on thread resume */
int lxtsd_is_vforked;
lx_exit_type_t lxtsd_exit;
int lxtsd_exit_status;
ucontext_t lxtsd_exit_context;
Expand Down

0 comments on commit 4af82cf

Please sign in to comment.