Skip to content

Commit

Permalink
[input] push and pop mutex cleanup around cancellation points #2837
Browse files Browse the repository at this point in the history
  • Loading branch information
dankamongmen committed Jan 24, 2025
1 parent c11efe8 commit 50f1854
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ rearrangements of Notcurses.

* 3.0.14 (not yet released)
* `ncplane_family_destroy()` has been added to the API.
* Added some `foot` capabilities. Recognize `ghostty` and bless its
quadrants/sextants implementations.
* A bug introduced sometime in 2022 that caused unpredictable
hangs on exit was resolved (#2837), yay!

* 3.0.13 (2025-01-11)
* Fix regression when building with `USE_CXX=off`.
Expand Down
17 changes: 15 additions & 2 deletions src/lib/in.c
Original file line number Diff line number Diff line change
Expand Up @@ -2668,6 +2668,11 @@ int inputready_fd(const inputctx* ictx){
#endif
}

static void
cleanup_mutex(void* v){
pthread_mutex_unlock(v);
}

static inline uint32_t
internal_get(inputctx* ictx, const struct timespec* ts, ncinput* ni){
uint32_t id;
Expand All @@ -2690,10 +2695,16 @@ internal_get(inputctx* ictx, const struct timespec* ts, ncinput* ni){
}
return NCKEY_EOF;
}
int r;
pthread_cleanup_push(cleanup_mutex, &ictx->ilock);
if(ts == NULL){
pthread_cond_wait(&ictx->icond, &ictx->ilock);
r = pthread_cond_wait(&ictx->icond, &ictx->ilock);
}else{
int r = pthread_cond_timedwait(&ictx->icond, &ictx->ilock, ts);
r = pthread_cond_timedwait(&ictx->icond, &ictx->ilock, ts);
}
pthread_cleanup_pop(0);
if(r){
pthread_mutex_unlock(&ictx->ilock);
if(r == ETIMEDOUT){
pthread_mutex_unlock(&ictx->ilock);
if(ni){
Expand Down Expand Up @@ -2728,9 +2739,11 @@ internal_get(inputctx* ictx, const struct timespec* ts, ncinput* ni){
logtrace("draining event readiness pipe %d", ictx->ivalid);
#ifndef __MINGW32__
char c;
pthread_cleanup_push(cleanup_mutex, &ictx->ilock);
while(read(ictx->readypipes[0], &c, sizeof(c)) == 1){
// FIXME accelerate?
}
pthread_cleanup_pop(0);
#else
// we ought be draining this, but it breaks everything, as we can't easily
// do nonblocking input from a pipe in windows, augh...
Expand Down

0 comments on commit 50f1854

Please sign in to comment.