Blob Blame Raw
diff --git a/src/libply/ply-terminal-session.c b/src/libply/ply-terminal-session.c
index 82e0c00..2a4ca8d 100644
--- a/src/libply/ply-terminal-session.c
+++ b/src/libply/ply-terminal-session.c
@@ -56,6 +56,7 @@ struct _ply_terminal_session
 
   uint32_t is_running : 1;
   uint32_t console_is_redirected : 1;
+  uint32_t created_terminal_device : 1;
 };
 
 static bool ply_terminal_session_open_console (ply_terminal_session_t *session);
@@ -325,6 +326,8 @@ ply_terminal_session_attach (ply_terminal_session_t               *session,
           ply_trace ("could not create pseudo-terminal: %m");
           return false;
         }
+
+      session->created_terminal_device = true;
     }
 
   if (should_redirect_console)
@@ -364,6 +367,13 @@ ply_terminal_session_detach (ply_terminal_session_t       *session)
       ply_terminal_session_unredirect_console (session);
     }
 
+  if (session->created_terminal_device)
+    {
+      ply_trace ("ptmx wasn't originally passed in, destroying created one");
+      ply_terminal_destroy_device (session->terminal);
+      session->created_terminal_device = false;
+    }
+
   session->output_handler = NULL;
   session->done_handler = NULL;
   session->user_data = NULL;