This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 295a8572522735f94c458adc568d84570192f304
Author: Kai Cheng <[email protected]>
AuthorDate: Wed Sep 16 18:05:21 2026 +0800

    pty: close the master pipes when its last reference goes away
    
    A pseudo terminal created with pty_register() never tells its slave that
    the master is gone.  A program on the slave side keeps polling a device
    whose peer has vanished and is given neither EOF nor POLLHUP.
    
    pty_close() closes the pipe ends a side owns once its last open reference
    goes away, and works that out from the inode reference count:
    
      if ((!dev->pd_master && atomic_read(&inode->i_crefs) == 2) ||
           (dev->pd_master && atomic_read(&inode->i_crefs) == 1))
    
    A device still in the VFS namespace carries one reference for its
    registration - inode_reserve() sets i_crefs to one - and every open adds
    another, so its last open reference is the second one.  That is what the
    slave branch checks.  The master branch checks for the first one instead,
    which only holds for a SUSv1 pair: ptmx_open() unlinks /dev/ptyN as soon
    as it has opened it, so there the open is the only reference left.  A
    master registered by pty_register() keeps its node, its count is two, and
    the branch never fires - the pipes stay open and the slave goes on seeing
    a writer that no longer exists.
    
    Take the flow into account instead of assuming one of them.
    
    Found with a Bluetooth SPP to pseudo terminal bridge carrying adbd on the
    slave side: killing the bridge left adbd polling a dead device forever,
    its read callback was not entered again at all.  With this, adbd is given
    UV_EOF a millisecond after the bridge goes away, drops the session and
    reattaches, and the link works again once the bridge is back.
    
    Signed-off-by: Kai Cheng <[email protected]>
---
 drivers/serial/pty.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/drivers/serial/pty.c b/drivers/serial/pty.c
index 4d1b3fddfec..8c808a61c13 100644
--- a/drivers/serial/pty.c
+++ b/drivers/serial/pty.c
@@ -362,10 +362,15 @@ static int pty_close(FAR struct file *filep)
       return ret;
     }
 
-  /* Check if the decremented inode reference count would go to zero */
+  /* Check if the decremented inode reference count would go to zero.  A
+   * device still in the VFS namespace holds one reference for its
+   * registration, so its last open reference is the second one.  The master
+   * of a SUSv1 pair is the exception: ptmx_open() unlinks it as soon as it
+   * has opened it, leaving the open itself as the only reference.
+   */
 
-  if ((!dev->pd_master && atomic_read(&inode->i_crefs) == 2) ||
-       (dev->pd_master && atomic_read(&inode->i_crefs) == 1))
+  if (atomic_read(&inode->i_crefs) ==
+      ((dev->pd_master && devpair->pp_susv1) ? 1 : 2))
     {
       /* Did the (single) master just close its reference? */
 

Reply via email to