From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Alan Third Newsgroups: gmane.emacs.bugs Subject: bug#25265: make-thread crashes in OS X 10.6 Date: Tue, 2 May 2017 21:49:35 +0100 Message-ID: <20170502204935.GA79100@breton.holly.idiocy.org> References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="17pEHd4RhPHOinZp" Content-Transfer-Encoding: 8bit X-Trace: blaine.gmane.org 1493758213 24176 195.159.176.226 (2 May 2017 20:50:13 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 2 May 2017 20:50:13 +0000 (UTC) User-Agent: Mutt/1.7.2 (2016-11-26) Cc: 25265@debbugs.gnu.org To: "Charles A. Roelli" Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Tue May 02 22:50:09 2017 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d5ekK-0006BX-Is for geb-bug-gnu-emacs@m.gmane.org; Tue, 02 May 2017 22:50:08 +0200 Original-Received: from localhost ([::1]:33628 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d5ekQ-0004PK-6F for geb-bug-gnu-emacs@m.gmane.org; Tue, 02 May 2017 16:50:14 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:54200) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d5ekI-0004MW-Ed for bug-gnu-emacs@gnu.org; Tue, 02 May 2017 16:50:08 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d5ekF-0005Q7-6v for bug-gnu-emacs@gnu.org; Tue, 02 May 2017 16:50:06 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:53387) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1d5ekF-0005Ps-1j for bug-gnu-emacs@gnu.org; Tue, 02 May 2017 16:50:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1d5ekE-0002Ov-9X for bug-gnu-emacs@gnu.org; Tue, 02 May 2017 16:50:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Alan Third Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 02 May 2017 20:50:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 25265 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 25265-submit@debbugs.gnu.org id=B25265.14937581869202 (code B ref 25265); Tue, 02 May 2017 20:50:02 +0000 Original-Received: (at 25265) by debbugs.gnu.org; 2 May 2017 20:49:46 +0000 Original-Received: from localhost ([127.0.0.1]:51586 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d5ejy-0002OL-5s for submit@debbugs.gnu.org; Tue, 02 May 2017 16:49:46 -0400 Original-Received: from mail-wm0-f49.google.com ([74.125.82.49]:34501) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d5eju-0002O6-RE for 25265@debbugs.gnu.org; Tue, 02 May 2017 16:49:43 -0400 Original-Received: by mail-wm0-f49.google.com with SMTP id r190so31025531wme.1 for <25265@debbugs.gnu.org>; Tue, 02 May 2017 13:49:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20161025; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=3XmEjZTV7W/ic+vuZzIkwU2X2lL+DwbPYgIEUV61lqE=; b=ReBZYvkueBwlE3VBQzuWIKt42vK2QrNa0BXM0ZiWyQb4Jk0GYsLsClK1ophXHHjsGi T3SJlPswANHCyRI/WOoaNtzSdX11pQlj+t8yCASW2aeMN2k/0o882Gw7n21AaTywVfB0 lPY5IIO6gyI13bVk54S/hkCt8eAseZz4g7/5x5Lfnv9wBtno/V+H1MbszKBVPwHGjJq5 jIlVT49B59ugKcKS1ozIOEoz5+huKnkZz3Rrm20dcjrv6hHY3hbBXjPZyoIRUcZRa4pG W79Ky0Rf4hcdEkltgXltOoAGMO+Zx7YtjaqkzvevIikHWFBotvTFe6UE6K9jMtQPNDBq 1T1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:from:to:cc:subject:message-id :references:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=3XmEjZTV7W/ic+vuZzIkwU2X2lL+DwbPYgIEUV61lqE=; b=P+l+wcF4FcXrceE8S+1tQQKvZj34EIszCocXlRA32a4m8igINYM52jmdEHv8Eo8zUO 6ExELeGahCRknATQftYhnjKtVEo/tc1ad47aVdPJJN1wjxdj2Y67RkpsGYG02WR7P1y0 o4budZjfQkeSEWIn48O9lODq49x1DTPoR9xjNmQcFy9+TkFcQ6s7fK2cqijBMTP/kkt1 hyn0Dkrqpr9JQkVbcnUBRPDqOdYvPqeikiOURDYgWSs7ifPfH8EZ4GLlcxmWQ+lIWG4I oxRbUf6uhi+tMjhTtdeVLNS4susa/JzxdCMNVwFsvbf/G9eGPpXYQTTjkT/IlUkTFmiZ C8ww== X-Gm-Message-State: AN3rC/5xGIb2kCdG3uzjD6we53YBYoGQkLqAXcOMMFh15sobCp0CgaiV z6Uz42r9JFFm3g== X-Received: by 10.28.216.85 with SMTP id p82mr3193946wmg.91.1493758177030; Tue, 02 May 2017 13:49:37 -0700 (PDT) Original-Received: from breton.holly.idiocy.org (ip6-2001-08b0-03f8-8129-50f6-0664-c41f-3461.holly.idiocy.org. [2001:8b0:3f8:8129:50f6:664:c41f:3461]) by smtp.gmail.com with ESMTPSA id 39sm19724416wru.50.2017.05.02.13.49.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 02 May 2017 13:49:35 -0700 (PDT) Content-Disposition: inline In-Reply-To: X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:132203 Archived-At: --17pEHd4RhPHOinZp Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit On Sat, Dec 24, 2016 at 12:06:45PM +0100, Charles A. Roelli wrote: > Running Emacs -Q on the latest commit (e36a3882), > > M-: (make-thread (lambda () "string")) > > appears to hang Emacs immediately. I’ve been working on this on and off for a while now, and I just can’t fix it. I’ve attached two patches that together are the best I’ve managed to achieve, but unfortunately it randomly freezes up with 100% CPU usage. I’ve not yet managed to work out what it’s doing when it goes into the 100% CPU loop. I can only assume I’ve missed some crucial case in ns_select or something. The first patch stops the NS port from using SIGIO, as it seems to be the source of a number of problems. The second removes the NS event loop in ns_select as it requires block_input/unblock_input wrapped around it, but that’s what’s causing the crash in make-thread. Instead it just looks for whether there is a new NS event as I don’t think that requires blocking input. -- Alan Third --17pEHd4RhPHOinZp Content-Type: text/plain; charset=us-ascii Content-Description: Remove SIGIO stuff Content-Disposition: attachment; filename="0001-Don-t-use-SIGIO-in-NS.patch" >From bff3dbfe43a24ee924edd777a7a876b627f94f11 Mon Sep 17 00:00:00 2001 From: Alan Third Date: Sat, 29 Apr 2017 23:35:16 +0100 Subject: [PATCH] Don't use SIGIO in NS * src/keyboard.c (handle_user_signal) [HAVE_NS]: Don't handle SIGIO. * src/keyboard.h (handle_input_available_signal) [HAVE_NS]: Make available to external code. * src/nsterm.m (hold_event, ns_Select): Call handle_input_available_signal directly. * src/sysdep.c (emacs_sigaction_init) [HAVE_NS]: Ignore SIGIO. --- src/keyboard.c | 3 ++- src/keyboard.h | 11 +++++++++++ src/nsterm.m | 4 ++-- src/sysdep.c | 2 +- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/keyboard.c b/src/keyboard.c index c9fa2a9f5e..d22f7b08c6 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -7264,7 +7264,8 @@ handle_user_signal (int sig) } p->npending++; -#ifdef USABLE_SIGIO +#if defined (USABLE_SIGIO) && !defined (HAVE_NS) + /* Dont use the interrupt handler in NS. */ if (interrupt_input) handle_input_available_signal (sig); else diff --git a/src/keyboard.h b/src/keyboard.h index 2219c01135..29279bef86 100644 --- a/src/keyboard.h +++ b/src/keyboard.h @@ -495,6 +495,17 @@ extern void mark_kboards (void); extern const char *const lispy_function_keys[]; #endif +#ifdef HAVE_NS +/* The NS port only uses SIGIO internally, and even then only in two + places in the code, but it causes crashes if certain code is not + properly wrapped in block_input/unblock_input. + + Since it's only used internally, we should be able to disable it, + and call the SIGIO handler directly. + */ +extern void handle_input_available_signal (int sig); +#endif + extern char const DEV_TTY[]; INLINE_HEADER_END diff --git a/src/nsterm.m b/src/nsterm.m index c22c5a70ba..00b7a89472 100644 --- a/src/nsterm.m +++ b/src/nsterm.m @@ -472,7 +472,7 @@ - (NSColor *)colorUsingDefaultColorSpace hold_event_q.q[hold_event_q.nr++] = *event; /* Make sure ns_read_socket is called, i.e. we have input. */ - raise (SIGIO); + handle_input_available_signal (SIGIO); send_appdefined = YES; } @@ -4289,7 +4289,7 @@ in certain situations (rapid incoming events). if (hold_event_q.nr > 0) { /* We already have events pending. */ - raise (SIGIO); + handle_input_available_signal (SIGIO); errno = EINTR; return -1; } diff --git a/src/sysdep.c b/src/sysdep.c index 91b2a5cb94..70fdf92964 100644 --- a/src/sysdep.c +++ b/src/sysdep.c @@ -1606,7 +1606,7 @@ emacs_sigaction_init (struct sigaction *action, signal_handler_t handler) { sigaddset (&action->sa_mask, SIGINT); sigaddset (&action->sa_mask, SIGQUIT); -#ifdef USABLE_SIGIO +#if defined (USABLE_SIGIO) && !defined (HAVE_NS) sigaddset (&action->sa_mask, SIGIO); #endif } -- 2.12.0 --17pEHd4RhPHOinZp Content-Type: text/plain; charset=us-ascii Content-Description: Remove NS event loop from ns_select Content-Disposition: attachment; filename="0001-Remove-event-loop-from-ns_select.patch" >From f759adb30eaf12af474057ebbc98f5f76cc590bf Mon Sep 17 00:00:00 2001 From: Alan Third Date: Sun, 30 Apr 2017 10:04:16 +0100 Subject: [PATCH] Remove event loop from ns_select * src/nsterm.m (ns_select): Remove event processing loop and replace with simple test for a new event. (ns_send_appdefined): Remove redundant timer code. --- src/nsterm.m | 92 ++++++++++++++++++++++++++++-------------------------------- 1 file changed, 43 insertions(+), 49 deletions(-) diff --git a/src/nsterm.m b/src/nsterm.m index 00b7a89472..462ab176c9 100644 --- a/src/nsterm.m +++ b/src/nsterm.m @@ -282,7 +282,6 @@ - (NSColor *)colorUsingDefaultColorSpace static BOOL send_appdefined = YES; #define NO_APPDEFINED_DATA (-8) static int last_appdefined_event_data = NO_APPDEFINED_DATA; -static NSTimer *timed_entry = 0; static NSTimer *scroll_repeat_entry = nil; static fd_set select_readfds, select_writefds; enum { SELECT_HAVE_READ = 1, SELECT_HAVE_WRITE = 2, SELECT_HAVE_TMO = 4 }; @@ -4074,14 +4073,6 @@ in certain situations (rapid incoming events). /* We only need one NX_APPDEFINED event to stop NXApp from running. */ send_appdefined = NO; - /* Don't need wakeup timer any more */ - if (timed_entry) - { - [timed_entry invalidate]; - [timed_entry release]; - timed_entry = nil; - } - nxev = [NSEvent otherEventWithType: NSEventTypeApplicationDefined location: NSMakePoint (0, 0) modifierFlags: 0 @@ -4277,9 +4268,11 @@ in certain situations (rapid incoming events). { int result; int t, k, nr = 0; - struct input_event event; char c; + NSDate *timeout_date = nil; + NSEvent *ns_event; + NSTRACE_WHEN (NSTRACE_GROUP_EVENTS, "ns_select"); #ifdef HAVE_NATIVE_FS @@ -4337,70 +4330,71 @@ in certain situations (rapid incoming events). /* Inform fd_handler that select should be called */ c = 'g'; emacs_write_sig (selfds[1], &c, 1); + /* We rely on fd_handler timing out to cause + nextEventMatchingMask below to return, so set it's timeout to + an unreasonably long time. */ + timeout_date = [NSDate distantFuture]; } else if (nr == 0 && timeout) { - /* No file descriptor, just a timeout, no need to wake fd_handler */ + /* No file descriptor, just a timeout, no need to wake + fd_handler. Set nextEventMatchingMask timeout. */ double time = timespectod (*timeout); - timed_entry = [[NSTimer scheduledTimerWithTimeInterval: time - target: NSApp - selector: - @selector (timeout_handler:) - userInfo: 0 - repeats: NO] - retain]; - } - else /* No timeout and no file descriptors, can this happen? */ - { - /* Send appdefined so we exit from the loop */ - ns_send_appdefined (-1); + timeout_date = [NSDate dateWithTimeIntervalSinceNow: time]; } - block_input (); - ns_init_events (&event); - - [NSApp run]; + /* Listen for a new NSEvent. */ + ns_event = [NSApp nextEventMatchingMask: NSEventMaskAny + untilDate: timeout_date + inMode: NSDefaultRunLoopMode + dequeue: NO]; - ns_finish_events (); if (nr > 0 && readfds) { c = 's'; emacs_write_sig (selfds[1], &c, 1); } - unblock_input (); - - t = last_appdefined_event_data; - if (t != NO_APPDEFINED_DATA) + if (ns_event != nil) { - last_appdefined_event_data = NO_APPDEFINED_DATA; - - if (t == -2) + if ([ns_event type] == NSEventTypeApplicationDefined) { - /* The NX_APPDEFINED event we received was a timeout. */ - result = 0; + if ([ns_event data1] < 0) + { + /* The NX_APPDEFINED event we received was a timeout. */ + result = 0; + } + else + { + /* Received back from select () in fd_handler; copy the results */ + pthread_mutex_lock (&select_mutex); + if (readfds) *readfds = select_readfds; + if (writefds) *writefds = select_writefds; + pthread_mutex_unlock (&select_mutex); + result = [ns_event data1]; + } + + /* Remove the NX_APPDEFINED event from the queue as it's no + longer needed. */ + [NSApp nextEventMatchingMask: NSEventMaskAny + untilDate: nil + inMode: NSDefaultRunLoopMode + dequeue: YES]; } - else if (t == -1) + else { - /* The NX_APPDEFINED event we received was the result of - at least one real input event arriving. */ + /* A real NSEvent came in. */ + handle_input_available_signal (0); errno = EINTR; result = -1; } - else - { - /* Received back from select () in fd_handler; copy the results */ - pthread_mutex_lock (&select_mutex); - if (readfds) *readfds = select_readfds; - if (writefds) *writefds = select_writefds; - pthread_mutex_unlock (&select_mutex); - result = t; - } } else { errno = EINTR; result = -1; + /* Reading from the NSEvent queue timed out. */ + result = 0; } return result; -- 2.12.0 --17pEHd4RhPHOinZp--