--- a/lib/readutmp.c
+++ b/lib/readutmp.c
@@ -829,6 +829,25 @@
             {
               char *session = *session_ptr;
 
+              /* A closing session may outlive its leader, for example when
+                 a detached screen is still running.  Checking only whether
+                 the leader PID exists can then accept an unrelated process
+                 that has reused that PID.  */
+              if (options & READ_UTMP_CHECK_PIDS)
+                {
+                  char *state;
+                  if (sd_session_get_state (session, &state) >= 0)
+                    {
+                      bool closing = streq (state, "closing");
+                      free (state);
+                      if (closing)
+                        {
+                          free (session);
+                          continue;
+                        }
+                    }
+                }
+
               uint64_t start_usec;
               if (sd_session_get_start_time (session, &start_usec) < 0)
                 start_usec = 0;
