【问题标题】:getaddrinfo and gethostbyname crashing when called from child thread?从子线程调用时 getaddrinfo 和 gethostbyname 崩溃?
【发布时间】:2014-11-21 16:25:15
【问题描述】:

我们创建了一个在 Ubuntu 上运行的多线程、单核应用程序。

当我们从主进程调用getaddrinfogethostbyname 时,它不会崩溃。

但是,当我们从主进程创建线程并从创建的线程调用函数 getaddrinfogethostbyname 时,它总是崩溃。

请帮助。 请在下面找到调用堆栈:

#0  0xf7e9f890 in ?? () from /lib/i386-linux-gnu/libc.so.6
#1  0xf7e9fa73 in __res_ninit () from /lib/i386-linux-gnu/libc.so.6
#2  0xf7ea0a68 in __res_maybe_init () from /lib/i386-linux-gnu/libc.so.6
#3  0xf7e663be in ?? () from /lib/i386-linux-gnu/libc.so.6
#4  0xf7e696bb in getaddrinfo () from /lib/i386-linux-gnu/libc.so.6
#5  0x080c4e35 in mn_task_entry (args=0xa6c4130 <ipc_os_input_params>) at /home/nextg/Alps_RT/mn/src/mn_main.c:699
#6  0xf7fa5d78 in start_thread () from /lib/i386-linux-gnu/libpthread.so.0
#7  0xf7e9001e in clone () from /lib/i386-linux-gnu/libc.so.6

【问题讨论】:

  • 您没有显示任何代码。是时候学习如何使用 gdb了。
  • gethostbyname 不需要是可重入的,因此如果从多个线程调用可能不是线程安全的。如果您使用glibc 作为您的c 库,您可以尝试使用gethostbyname_r 看看是否可以解决问题。

标签: c linux multithreading getaddrinfo gethostbyname


【解决方案1】:

getaddrinfo 崩溃的原因是,进行调用的子线程没有足够的堆栈空间。

【讨论】:

    【解决方案2】:

    根据Syed Aslam 的报告,使用 ACE C++ 版本 6.5.1 库的类在使用 ACE_DEFAULT_THREAD_PRIORITY (1024*1024) 和默认 ACE_DEFAULT_THREAD_PRIORITY (1024*1024) 时会崩溃。 libxml2 模式解析需要永远,使用子线程 Segment Faulted 在调用 xmlNanoHTTPConnectHost 后尝试解析 schemaLocation

    ACE_Task 激活

    const ACE_TCHAR *thr_name[1];
    thr_name[0] = "Flerf";
    // libxml2-2.9.7/nanohttp.c:1133
    // gethostbyname will crash when child thread making the call 
    // has insufficient stack space.
    size_t stack_sizes[1] = {
      ACE_DEFAULT_THREAD_STACKSIZE * 100
    };
    
    const INT ret = this->activate (
      THR_NEW_LWP/*Light Weight Process*/ | THR_JOINABLE,
      1,
      0/*force_active*/,
      ACE_DEFAULT_THREAD_PRIORITY,
      -1/*grp_id*/,
      NULL/*task*/,
      NULL/*thread_handles[]*/,
      NULL/*stack[]*/,
      stack_sizes/*stack_size[]*/,
      NULL/*thread_ids[]*/,
      thr_name
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-01
      • 1970-01-01
      相关资源
      最近更新 更多