This is an automated email from the ASF dual-hosted git repository. acassis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 4bc2542a870abaf9bad7bcdc645c39a0c12f4662 Author: Marco Casaroli <[email protected]> AuthorDate: Fri Nov 27 21:17:15 2026 +0100 drivers/serial/pty: Close the pipe ends of both sides when the pair goes. The first open of a pseudo-terminal pair makes the two pipes of the pair, with the ends of both sides. The last close of a side closes only the ends of that side. When the master of /dev/ptmx was closed and its slave was never opened, the ends of the slave stayed open. The pair was freed, but its two pipes stayed allocated: each posix_openpt() and close() lost two pipes (about 1 KiB of kernel heap). A BSD pair (without CONFIG_PSEUDOTERM_SUSV1) stays after its last close, and its next open made new pipes over the ends that were still open: the same loss. pty_destroy() now closes the ends of both sides that are still open, and so does the last close of a pair that stays. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- drivers/serial/pty.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/drivers/serial/pty.c b/drivers/serial/pty.c index 1e699f678ca..7b341f16819 100644 --- a/drivers/serial/pty.c +++ b/drivers/serial/pty.c @@ -129,6 +129,7 @@ struct pty_devpair_s * Private Function Prototypes ****************************************************************************/ +static void pty_closepipes(FAR struct pty_devpair_s *devpair); static void pty_destroy(FAR struct pty_devpair_s *devpair); static int pty_pipe(FAR struct pty_devpair_s *devpair); static int pty_open(FAR struct file *filep); @@ -170,6 +171,26 @@ static const struct file_operations g_pty_fops = * Private Functions ****************************************************************************/ +/**************************************************************************** + * Name: pty_closepipes + * + * Description: + * Close the pipe ends of both sides. The first open of a pair makes both + * pipes, with the ends of both sides. The last close of a side closes + * only its own ends, so a side that was not opened (a master closed + * before an open of its slave) still holds its ends. file_close() does + * nothing for an end that is already closed. + * + ****************************************************************************/ + +static void pty_closepipes(FAR struct pty_devpair_s *devpair) +{ + file_close(&devpair->pp_master.pd_src); + file_close(&devpair->pp_master.pd_sink); + file_close(&devpair->pp_slave.pd_src); + file_close(&devpair->pp_slave.pd_sink); +} + /**************************************************************************** * Name: pty_destroy ****************************************************************************/ @@ -204,6 +225,10 @@ static void pty_destroy(FAR struct pty_devpair_s *devpair) unregister_driver(devname); } + /* Close the pipe ends that are still open */ + + pty_closepipes(devpair); + /* And free the device structure */ nxmutex_destroy(&devpair->pp_lock); @@ -401,9 +426,16 @@ static int pty_close(FAR struct file *filep) } else { - /* Otherwise just decrement the open count */ + /* Otherwise just decrement the open count. A pair that stays after + * its last close (BSD) makes new pipes at its next open: close the + * ends that a side that was not opened still holds. + */ devpair->pp_nopen--; + if (devpair->pp_nopen == 0) + { + pty_closepipes(devpair); + } } nxmutex_unlock(&devpair->pp_lock);
