emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

nanosleep autoconf check hangs forever

Open vadimkantorov opened this issue 5 years ago • 12 comments

I tried to build GNU diffutils with emscripten

checking for library containing nanosleep... none required
checking for working nanosleep..

During an out-of-tree configure, this nanosleep check hangs forever.

Here is a zip with conftest.c: diffutils.zip


          #include <errno.h>
          #include <limits.h>
          #include <signal.h>
          #if HAVE_SYS_TIME_H
           #include <sys/time.h>
          #endif
          #include <time.h>
          #include <unistd.h>
          #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
          #define TYPE_MAXIMUM(t)             ((t) (! TYPE_SIGNED (t)                   ? (t) -1                   : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1)))

          #if HAVE_DECL_ALARM
          static void
          check_for_SIGALRM (int sig)
          {
            if (sig != SIGALRM)
              _exit (1);
          }
          #endif

          int
          main ()
          {
            static struct timespec ts_sleep;
            static struct timespec ts_remaining;
            /* Test for major problems first.  */
            if (! nanosleep)
              return 2;
            ts_sleep.tv_sec = 0;
            ts_sleep.tv_nsec = 1;
            #if HAVE_DECL_ALARM
            {
              static struct sigaction act;
              act.sa_handler = check_for_SIGALRM;
              sigemptyset (&act.sa_mask);
              sigaction (SIGALRM, &act, NULL);
              alarm (1);
              if (nanosleep (&ts_sleep, NULL) != 0)
                return 3;
              /* Test for a minor problem: the handling of large arguments.  */
              ts_sleep.tv_sec = TYPE_MAXIMUM (time_t);
              ts_sleep.tv_nsec = 999999999;
              alarm (1);
              if (nanosleep (&ts_sleep, &ts_remaining) != -1)
                return 4;
              if (errno != EINTR)
                return 5;
              if (ts_remaining.tv_sec <= TYPE_MAXIMUM (time_t) - 10)
                return 6;
            }
            #else /* A simpler test for native Windows.  */
            if (nanosleep (&ts_sleep, &ts_remaining) < 0)
              return 3;
            #endif
            return 0;
          }                                             

vadimkantorov avatar Oct 02 '20 21:10 vadimkantorov

sleep check also hangs forever:

checking whether sleep is declared... yes
checking for working sleep...
#include <errno.h>
#include <unistd.h>
#include <signal.h>
static void
handle_alarm (int sig)
{
  if (sig != SIGALRM)
    _exit (2);
}

int
main (void)
{

    /* Failure to compile this test due to missing alarm is okay,
       since all such platforms (mingw) also lack sleep.  */
    unsigned int pentecost = 50 * 24 * 60 * 60; /* 50 days.  */
    unsigned int remaining;
    signal (SIGALRM, handle_alarm);
    alarm (1);
    remaining = sleep (pentecost);
    if (remaining > pentecost)
      return 3;
    if (remaining <= pentecost - 10)
      return 4;
    return 0;

  ;
  return 0;
}

diffutils.zip

vadimkantorov avatar Oct 02 '20 21:10 vadimkantorov

maybe it's actually a problem with signals

vadimkantorov avatar Oct 02 '20 21:10 vadimkantorov

Yes, looks like the sleep is for 50 days, so it never stops, and is never interrupted since we don't have signal support.

The issue is probably that we should fail with a compile-time error on not having the signal functions - but they have been useful for porting, which is why they exist, so I'm not sure what's best here.

kripken avatar Oct 05 '20 16:10 kripken

Maybe emulate alarm function / timer specifically? It should be possible with something like asyncify?

vadimkantorov avatar Oct 05 '20 17:10 vadimkantorov

How about failing at link time with an error like build with -s FAKE_SIGNALS if you want fake signal support? or do you think we want the portable fake signals on by default?

sbc100 avatar Oct 06 '20 01:10 sbc100

Also, adding busybox to tests could be a nice thing since it builds so many things under the hood: like in https://github.com/tbfleming/em-busybox/commits/master

vadimkantorov avatar Oct 06 '20 12:10 vadimkantorov

@sbc100

I think it would be ok to error on signals by default, as long as the error said exactly what to do to get the fake support (build with -s FAKE_SIGNALS etc.). It would be a breaking change but one that's not surprising at least.

kripken avatar Oct 06 '20 20:10 kripken

Maybe also alarm function could crash (this is related but not equivalent ot signal handling)

vadimkantorov avatar Oct 07 '20 12:10 vadimkantorov

This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.

stale[bot] avatar Apr 17 '22 22:04 stale[bot]

I was also bitten by this when I tried to compile GNU sed. I was able to work around this with:

emconfigure ../configure ac_cv_have_decl_alarm=no gl_cv_func_sleep_works=yes

Vvalter avatar Aug 06 '22 13:08 Vvalter

I am still experiencing this issue with version 4.0.7-git while trying to build libunistring.

Steps to reproduce

# Download source
curl -O https://mirror.ibcp.fr/pub/gnu/libunistring/libunistring-1.3
tar -xvf libunistring-1.3.tar.gz

# Configure
cd libunistring-1.3
emconfigure ./configure

Solution

For anyone coming across this issue, I managed to solve this by disabling the checks:

emconfigure ./configure \
ac_cv_search_nanosleep=no \
ac_cv_have_decl_sleep=no \
--disable-threads \
# ...

Jomy10 avatar Apr 23 '25 14:04 Jomy10

I am still experiencing this issue with version 4.0.7-git while trying to build libunistring.

@sbc100 should this be reopened?

@Jomy10 the checks code themselves is equivalent to my codes above?

vadimkantorov avatar Apr 23 '25 19:04 vadimkantorov