fork/exec 이후 deadlock

dao_3651의 이미지

C 데몬 중에서 system 함수를 사용 하여 스크립트를 수행 하는 프로세스가 있습니다.

그런데, socket등이 상속 되어 이슈가 되어서 다음과 같은 순서로 변경 하였습니다.
fork --> 자원 해제 --> exec (child)
`--> waitpid (parent)

그런데 이렇게 하니 드물게 child side 에서 deadlock이 발생 하네요.

#0 0x00007f6e9f67009e in __lll_lock_wait_private () from /lib64/libc.so.6
#1 0x00007f6e9f615cdd in _L_lock_2163 () from /lib64/libc.so.6
#2 0x00007f6e9f615a97 in __tz_convert () from /lib64/libc.so.6
#3 0x00007f6e9f65ca9d in __vsyslog_chk () from /lib64/libc.so.6
#4 0x00007f6e9f65d0d0 in syslog () from /lib64/libc.so.6
#5 0x00007f6ea092ab96 in snoopy_log () from /usr/local/lib64/snoopy.so
#6 0x00007f6ea092abe9 in execv () from /usr/local/lib64/snoopy.so
#7 0x0000000000590b59 in tr_system ()
#8 0x000000000044455b in imr_SendReserve ()
#9 0x00000000004161f2 in __ReserveServer ()
#10 0x00007f6ea070aa51 in start_thread () from /lib64/libpthread.so.0
#11 0x00007f6e9f66093d in clone () from /lib64/libc.so.6

parent side 에서는 waitpid으로 인하여 진행이 안되고 있고요.

소스는 다음과 같습니다.

int tr_system(const char * ls_command)
{
    pid_t pid;
    int   status;
    int   tm_delta;
    time_t wait_end_tm;
    time_t wait_start_tm;
    int   n_close_socket = 0;
    char *args[] = {"/bin/sh", "-c", ls_command, NULL}; 
 
    DEBUGH("[%s] Enter : command='%s'", __func__, ls_command);
 
    if (ls_command == NULL || strlen(ls_command) == 0) {
        ERRN("[%s] Error, Invalid input parameter : command is NULL zero length ptr=%p", __func__, ls_command);
        return -1;
    }
 
    pid = fork();
 
    if(pid == -1) {
        ERRN("[%s] Error, Fail to fork : command='%s'", __func__, ls_command);
        return -1;
    }
 
    if(pid == 0) {
        n_close_socket = close_opened_fd(); /* child process : Close ALL socket fd */
        if (execv(args[0], args) == -1) {
            exit(127);
        }
        exit(127);
    }
    else {
        wait_start_tm = time(NULL);
        waitpid(pid, &status, 0);
        wait_end_tm = time(NULL);
        tm_delta    = wait_end_tm - wait_start_tm;
        status      = htons(status);
        if (127 == status) {
            ERRN("[%s] Error, Fail to execv : subs_pid=%d, status=%d, command='%s'", __func__, pid, status, ls_command);
        }
        else {
            DEBUGH("[%s] Execute command ended : escapes_time=%d, subs_pid=%d, status=%d, command='%s'", __func__, tm_delta, pid, status, ls_command);
        }
    }
 
    return status;
}

검색을 해보니, parent side의 다른 thread에서 glibc-lock을 획득 한 상태에서 fork가 발생하여 child process가 생성 될 경우 이 lock을 해재 할 방법이 없어(해당 thread가 없으니..) 발생 하는 이슈라고 하는데 해결책을 찾기가 막막하네요 ㅠㅠ

jick의 이미지

socket fd가 살아남는 게 문제라면 소켓을 열 때 FD_CLOEXEC를 사용하시는 게 좋을 것 같습니다.

close_opened_fd가 뭐하는 함수인지는 안 나와있어서-_- 모르겠지만, fork를 한 다음에 리소스를 해제하는 건 보시다시피 문제가 생기는 경우가 많습니다. (뭐 일반론이고 100% 항상 그렇다는 건 아닙니다만..)

dao_3651의 이미지

fork & exec 대신 기존대로 system() popen()사용 하도록 하고, 소켓 생성시 FD_CLOEXEC옵션을 주는편이 좋겟네요.

glibc 의 system 함수 구현을봐도 fork 이후에 여러가지 처리를 하는데.. 제 구현에서는 모두 빠져 있어서 위험한 코드가 되었던것같습니다. ㅠㅠ

도움주셔서 감사합니다.

익명 사용자의 이미지

https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_atfork.html

There are at least two serious problems with the semantics of fork() in a multi-threaded program. One problem has to do with state (for example, memory) covered by mutexes. Consider the case where one thread has a mutex locked and the state covered by that mutex is inconsistent while another thread calls fork(). In the child, the mutex is in the locked state (locked by a nonexistent thread and thus can never be unlocked). Having the child simply reinitialize the mutex is unsatisfactory since this approach does not resolve the question about how to correct or otherwise deal with the inconsistent state in the child.

이라고 되있네요 ㅎㅎ

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.