enhanced the vm cleanup to be more error resistent

This commit is contained in:
hyunghwan.chung 2018-02-02 14:37:49 +00:00
parent 461e68c5b4
commit e70b424826
4 changed files with 43 additions and 7 deletions

View File

@ -174,6 +174,7 @@ AC_CHECK_FUNCS([makecontext swapcontext getcontext setcontext])
AC_CHECK_FUNCS([clock_nanosleep nanosleep usleep]) AC_CHECK_FUNCS([clock_nanosleep nanosleep usleep])
AC_CHECK_FUNCS([snprintf _vsnprintf _vsnwprintf strerror_r]) AC_CHECK_FUNCS([snprintf _vsnprintf _vsnwprintf strerror_r])
AC_CHECK_FUNCS([accept4]) AC_CHECK_FUNCS([accept4])
AC_CHECK_FUNCS([isatty])
AC_CHECK_LIB([dyncall_s], [dcNewCallVM], AC_CHECK_LIB([dyncall_s], [dcNewCallVM],
[ [

View File

@ -125,7 +125,7 @@ static MOO_INLINE const char* proc_state_to_string (int state)
# define __PRIMITIVE_NAME__ (&__FUNCTION__[0]) # define __PRIMITIVE_NAME__ (&__FUNCTION__[0])
#endif #endif
static int delete_from_sem_io (moo_t* moo, moo_oop_semaphore_t sem); static int delete_from_sem_io (moo_t* moo, moo_oop_semaphore_t sem, int force);
static void signal_io_semaphore (moo_t* moo, moo_ooi_t io_handle, moo_ooi_t mask); static void signal_io_semaphore (moo_t* moo, moo_ooi_t io_handle, moo_ooi_t mask);
static int send_message (moo_t* moo, moo_oop_char_t selector, int to_super, moo_ooi_t nargs); static int send_message (moo_t* moo, moo_oop_char_t selector, int to_super, moo_ooi_t nargs);
static int send_message_with_str (moo_t* moo, const moo_ooch_t* nameptr, moo_oow_t namelen, int to_super, moo_ooi_t nargs); static int send_message_with_str (moo_t* moo, const moo_ooch_t* nameptr, moo_oow_t namelen, int to_super, moo_ooi_t nargs);
@ -168,12 +168,14 @@ static MOO_INLINE void vm_cleanup (moo_t* moo)
if (moo->sem_io_tuple[sem_io_index].sem[MOO_SEMAPHORE_IO_TYPE_INPUT]) if (moo->sem_io_tuple[sem_io_index].sem[MOO_SEMAPHORE_IO_TYPE_INPUT])
{ {
delete_from_sem_io (moo, moo->sem_io_tuple[sem_io_index].sem[MOO_SEMAPHORE_IO_TYPE_INPUT]); delete_from_sem_io (moo, moo->sem_io_tuple[sem_io_index].sem[MOO_SEMAPHORE_IO_TYPE_INPUT], 1);
} }
if (moo->sem_io_tuple[sem_io_index].sem[MOO_SEMAPHORE_IO_TYPE_OUTPUT]) if (moo->sem_io_tuple[sem_io_index].sem[MOO_SEMAPHORE_IO_TYPE_OUTPUT])
{ {
delete_from_sem_io (moo, moo->sem_io_tuple[sem_io_index].sem[MOO_SEMAPHORE_IO_TYPE_OUTPUT]); delete_from_sem_io (moo, moo->sem_io_tuple[sem_io_index].sem[MOO_SEMAPHORE_IO_TYPE_OUTPUT], 1);
} }
MOO_ASSERT (moo, moo->sem_io_map[i] <= -1);
} }
else else
{ {
@ -1247,7 +1249,7 @@ static int add_to_sem_io (moo_t* moo, moo_oop_semaphore_t sem, moo_ooi_t io_hand
return 0; return 0;
} }
static int delete_from_sem_io (moo_t* moo, moo_oop_semaphore_t sem) static int delete_from_sem_io (moo_t* moo, moo_oop_semaphore_t sem, int force)
{ {
moo_ooi_t index; moo_ooi_t index;
moo_ooi_t new_mask, io_handle, io_type; moo_ooi_t new_mask, io_handle, io_type;
@ -1280,7 +1282,15 @@ static int delete_from_sem_io (moo_t* moo, moo_oop_semaphore_t sem)
if (x <= -1) if (x <= -1)
{ {
MOO_LOG3 (moo, MOO_LOG_WARN, "Failed to delete an IO semaphore at index %zd of type %d on handle %zd\n", index, (int)io_type, io_handle); MOO_LOG3 (moo, MOO_LOG_WARN, "Failed to delete an IO semaphore at index %zd of type %d on handle %zd\n", index, (int)io_type, io_handle);
return -1; if (!force) return -1;
/* NOTE:
* this means there could be some issue handling the file handles.
* the file handle might have been closed before reaching here.
* assuming the callback works correctly, it's not likely that the
* underlying operating system returns failure for no reason.
* i should inspect the overall vm implementation */
MOO_LOG3 (moo, MOO_LOG_ERROR, "Forcibly unmapping the handle %zd despite failure\n", index, (int)io_type, io_handle);
} }
MOO_LOG3 (moo, MOO_LOG_DEBUG, "Deleted an IO semaphore at index %zd of type %d on handle %zd\n", index, (int)io_type, io_handle); MOO_LOG3 (moo, MOO_LOG_DEBUG, "Deleted an IO semaphore at index %zd of type %d on handle %zd\n", index, (int)io_type, io_handle);
@ -2613,7 +2623,7 @@ static moo_pfrc_t pf_system_remove_semaphore (moo_t* moo, moo_ooi_t nargs)
MOO_ASSERT (moo, MOO_OOP_IS_SMOOI(sem->io_handle) && MOO_OOP_TO_SMOOI(sem->io_handle) >= 0); MOO_ASSERT (moo, MOO_OOP_IS_SMOOI(sem->io_handle) && MOO_OOP_TO_SMOOI(sem->io_handle) >= 0);
MOO_ASSERT (moo, MOO_OOP_IS_SMOOI(sem->io_type)); MOO_ASSERT (moo, MOO_OOP_IS_SMOOI(sem->io_type));
if (delete_from_sem_io (moo, sem) <= -1) if (delete_from_sem_io(moo, sem, 0) <= -1)
{ {
const moo_ooch_t* oldmsg = moo_backuperrmsg(moo); const moo_ooch_t* oldmsg = moo_backuperrmsg(moo);
moo_seterrbfmt (moo, moo->errnum, "cannot delete the handle %zd from the multiplexer - %js", MOO_OOP_TO_SMOOI(sem->io_handle), oldmsg); moo_seterrbfmt (moo, moo->errnum, "cannot delete the handle %zd from the multiplexer - %js", MOO_OOP_TO_SMOOI(sem->io_handle), oldmsg);

View File

@ -614,7 +614,12 @@ moo_ooi_t moo_logbfmt (moo_t* moo, moo_oow_t mask, const moo_bch_t* fmt, ...)
if (moo->log.default_type_mask & MOO_LOG_ALL_TYPES) if (moo->log.default_type_mask & MOO_LOG_ALL_TYPES)
{ {
mask &= ~MOO_LOG_UNTYPED; /* if a type is given, it's not untyped any more.
* mask off the UNTYPED bit */
mask &= ~MOO_LOG_UNTYPED;
/* if the default_type_mask has the UNTYPED bit on,
* it'll get turned back on */
mask |= (moo->log.default_type_mask & MOO_LOG_ALL_TYPES); mask |= (moo->log.default_type_mask & MOO_LOG_ALL_TYPES);
} }

View File

@ -197,6 +197,7 @@ struct xtn_t
int logfd; int logfd;
int logmask; int logmask;
int logfd_istty;
#if defined(_WIN32) #if defined(_WIN32)
HANDLE waitable_timer; HANDLE waitable_timer;
@ -777,6 +778,14 @@ static void log_write (moo_t* moo, moo_oow_t mask, const moo_ooch_t* msg, moo_oo
write_all (logfd, ts, tslen); write_all (logfd, ts, tslen);
} }
if (xtn->logfd_istty)
{
if (mask & MOO_LOG_FATAL) write_all (logfd, "\x1B[1;31m", 7);
else if (mask & MOO_LOG_ERROR) write_all (logfd, "\x1B[1;36m", 7);
else if (mask & MOO_LOG_WARN) write_all (logfd, "\x1B[1;33m", 7);
}
#if defined(MOO_OOCH_IS_UCH) #if defined(MOO_OOCH_IS_UCH)
msgidx = 0; msgidx = 0;
while (len > 0) while (len > 0)
@ -815,6 +824,11 @@ static void log_write (moo_t* moo, moo_oow_t mask, const moo_ooch_t* msg, moo_oo
write_all (logfd, msg, len); write_all (logfd, msg, len);
#endif #endif
if (xtn->logfd_istty)
{
if (mask & (MOO_LOG_FATAL | MOO_LOG_ERROR | MOO_LOG_WARN)) write_all (logfd, "\x1B[0m", 4);
}
#endif #endif
} }
@ -2022,6 +2036,7 @@ static void close_moo (moo_t* moo)
{ {
close (xtn->logfd); close (xtn->logfd);
xtn->logfd = -1; xtn->logfd = -1;
xtn->logfd_istty = 0;
} }
moo_close (moo); moo_close (moo);
} }
@ -2091,6 +2106,10 @@ static int handle_logopt (moo_t* moo, const moo_bch_t* str)
return -1; return -1;
} }
#if defined(HAVE_ISATTY)
xtn->logfd_istty = isatty(xtn->logfd))
#endif
if (str != xstr) moo_freemem (moo, xstr); if (str != xstr) moo_freemem (moo, xstr);
return 0; return 0;
} }
@ -2263,6 +2282,7 @@ int main (int argc, char* argv[])
xtn = moo_getxtn (moo); xtn = moo_getxtn (moo);
xtn->logfd = -1; xtn->logfd = -1;
xtn->logfd_istty = 0;
if (logopt) if (logopt)
{ {