This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch master
in repository terminology.

View the commit online.

commit c0ec5e65db5604a4a1c6ca59830f331156804ef6
Author: Cedric BAIL <[email protected]>
AuthorDate: Fri Nov 20 09:08:38 2026 -0600

    termpty: delete the pty fd handler before closing it
    
    A read error on the pty closed the fd before its handler was deleted.
    Ecore could then not take the fd off its epoll set, and the EBADF made
    it rebuild its whole main loop. Two such rebuilds before the next
    select can leave a freed handler on ecore's list of prepare callbacks,
    which the next select calls through.
    
    The read error path is reached when the slave side goes away while
    _handle_read() is still draining the pty, epoll having reported only
    EPOLLIN: several ptys whose shells die together with output pending can
    take it in the same iteration.
    
    termpty_read_error reads a pty whose slave is closed and counts the
    warnings ecore logs: without this change ecore warns that it rebuilds
    its loop. It checks the cause rather than the crash, which needs freed
    memory to be overwritten, something a unit test cannot pin.
---
 src/bin/termpty.c    | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 src/bin/tytest.c     |  1 +
 src/bin/unit_tests.h |  1 +
 3 files changed, 61 insertions(+), 2 deletions(-)

diff --git a/src/bin/termpty.c b/src/bin/termpty.c
index 34912d79..aec6993c 100644
--- a/src/bin/termpty.c
+++ b/src/bin/termpty.c
@@ -292,11 +292,13 @@ _handle_read(Termpty *ty, Eina_Bool false_on_empty)
                {
                   ERR("error while reading from tty slave fd: %s", strerror(errno));
                }
-             close(ty->fd);
-             ty->fd = -1;
+             /* ecore cannot take a closed fd off its epoll set: on the
+              * EBADF it rebuilds its whole main loop */
              if (ty->hand_fd)
                ecore_main_fd_handler_del(ty->hand_fd);
              ty->hand_fd = NULL;
+             close(ty->fd);
+             ty->fd = -1;
              return ECORE_CALLBACK_CANCEL;
           }
         if (len <= 0) break;
@@ -4573,4 +4575,59 @@ tytest_block_region_scrolls(void)
 }
 #endif
 
+#if defined(BINARY_TYTEST)
+#if defined(__linux__)
+static void
+_ty_log_count(const Eina_Log_Domain *d EINA_UNUSED,
+              Eina_Log_Level level EINA_UNUSED,
+              const char *file EINA_UNUSED, const char *fnc EINA_UNUSED,
+              int line EINA_UNUSED, const char *fmt EINA_UNUSED,
+              void *data, va_list args EINA_UNUSED)
+{
+   int *count = data;
+
+   (*count)++;
+}
+#endif
+
+/* A read error on the pty takes its handler off the main loop without
+ * making ecore rebuild the loop, which it logs as a warning. */
+int
+tytest_termpty_read_error(void)
+{
+#if defined(__linux__)
+   Termpty ty;
+   int slave, ecore_level, warnings = 0;
+
+   ecore_init();
+   memset(&ty, 0, sizeof(ty));
+   ty.pid = -1;
+   ty.fd = posix_openpt(O_RDWR | O_NOCTTY | O_CLOEXEC);
+   assert(ty.fd >= 0);
+   assert(grantpt(ty.fd) == 0);
+   assert(unlockpt(ty.fd) == 0);
+   slave = open(ptsname(ty.fd), O_RDWR | O_NOCTTY | O_CLOEXEC);
+   assert(slave >= 0);
+   /* with no slave left open, Linux fails reads on the master with EIO */
+   close(slave);
+   ty.hand_fd = ecore_main_fd_handler_add(ty.fd, ECORE_FD_READ, _cb_fd, &ty,
+                                          NULL, NULL);
+   assert(ty.hand_fd);
+
+   ecore_level = eina_log_domain_level_get("ecore");
+   eina_log_domain_level_set("ecore", EINA_LOG_LEVEL_WARN);
+   eina_log_print_cb_set(_ty_log_count, &warnings);
+   assert(_handle_read(&ty, EINA_FALSE) == ECORE_CALLBACK_CANCEL);
+   eina_log_print_cb_set(eina_log_print_cb_stderr, NULL);
+   eina_log_domain_level_set("ecore", ecore_level);
+
+   assert(!ty.hand_fd);
+   assert(ty.fd == -1);
+   assert(warnings == 0);
+   ecore_shutdown();
+#endif
+   return 0;
+}
+#endif
+
 #endif /* BINARY_TYFUZZ || BINARY_TYTEST */
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 3a986233..a74c2fbb 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -74,6 +74,7 @@ static struct {
        { "sync_change_cb_altscreen", tytest_sync_change_cb_altscreen},
        { "hl_links_free_full", tytest_hl_links_free_full},
        { "hl_links_exhausted", tytest_hl_links_exhausted},
+       { "termpty_read_error", tytest_termpty_read_error},
        { "percent_decode", tytest_percent_decode},
        { "sgr_58_no_leak", tytest_sgr_58_no_leak},
        { "xmodkeys_set", tytest_xmodkeys_set},
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index ebb6af50..04f5f7e3 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -47,6 +47,7 @@ int tytest_sync_change_cb_watchdog(void);
 int tytest_sync_change_cb_altscreen(void);
 int tytest_hl_links_free_full(void);
 int tytest_hl_links_exhausted(void);
+int tytest_termpty_read_error(void);
 int tytest_percent_decode(void);
 int tytest_sgr_58_no_leak(void);
 int tytest_xmodkeys_set(void);

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to