Skip to content

Commit db06620

Browse files
committed
Don't relaunch a slow-starting session while its process is alive
Port of rstudio/rstudio-pro#12967. An aged pending launch whose recorded pid is still running is no longer relaunched until the stale pending launch age; a duplicate would rebind the same session socket and fail on the first session's source database locks. Addresses #18572.
1 parent 36df1fc commit db06620

3 files changed

Lines changed: 80 additions & 6 deletions

File tree

‎src/cpp/server/ServerSessionManagerTests.cpp‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,49 @@ TEST(SessionManagerTest, RequestErrorClearsPendingLaunchWithoutRecordedPid)
214214
sessionManager().removePendingLaunch(context);
215215
}
216216

217+
TEST(SessionManagerTest, AgedPendingLaunchIsNotRelaunchedWhileInFlight)
218+
{
219+
sessionManager().setSessionLaunchFunction(countingLaunchFunction);
220+
s_launchCount = 0;
221+
222+
r_util::SessionContext liveContext("pending-launch-slow-live-process-user");
223+
EXPECT_TRUE(attemptLaunch(liveContext));
224+
sessionManager().notePendingLaunchPid(liveContext, ::getpid());
225+
226+
r_util::SessionContext noPidContext("pending-launch-slow-no-pid-user");
227+
EXPECT_TRUE(attemptLaunch(noPidContext));
228+
229+
r_util::SessionContext deadContext("pending-launch-slow-dead-pid-user");
230+
EXPECT_TRUE(attemptLaunch(deadContext));
231+
sessionManager().notePendingLaunchPid(
232+
deadContext, std::numeric_limits<PidType>::max());
233+
EXPECT_EQ(3, s_launchCount);
234+
235+
sessionManager().setPendingLaunchTimeouts(boost::posix_time::seconds(0),
236+
boost::posix_time::hours(1));
237+
238+
EXPECT_FALSE(attemptLaunch(liveContext));
239+
EXPECT_EQ(3, s_launchCount);
240+
241+
EXPECT_TRUE(attemptLaunch(noPidContext));
242+
EXPECT_EQ(4, s_launchCount);
243+
244+
EXPECT_TRUE(attemptLaunch(deadContext));
245+
EXPECT_EQ(5, s_launchCount);
246+
247+
sessionManager().setPendingLaunchTimeouts(boost::posix_time::seconds(0),
248+
boost::posix_time::seconds(0));
249+
250+
EXPECT_TRUE(attemptLaunch(liveContext));
251+
EXPECT_EQ(6, s_launchCount);
252+
253+
sessionManager().setPendingLaunchTimeouts(boost::posix_time::minutes(1),
254+
boost::posix_time::minutes(3));
255+
sessionManager().removePendingLaunch(liveContext);
256+
sessionManager().removePendingLaunch(noPidContext);
257+
sessionManager().removePendingLaunch(deadContext);
258+
}
259+
217260
} // namespace tests
218261
} // namespace server
219262
} // namespace rstudio

‎src/cpp/server/include/server/session/ServerSessionManager.hpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ class SessionManager
8282
// launch was recorded for a different process
8383
void removePendingLaunchForPid(const core::r_util::SessionContext& context, PidType pid, int exitStatus);
8484

85+
void setPendingLaunchTimeouts(const boost::posix_time::time_duration& launchWindow,
86+
const boost::posix_time::time_duration& staleAge);
87+
8588
// set a custom session launcher
8689
typedef boost::function<core::Error(
8790
boost::asio::io_context&,
@@ -125,6 +128,8 @@ class SessionManager
125128
boost::mutex launchesMutex_;
126129
typedef std::map<core::r_util::SessionContext, PendingLaunch> LaunchMap;
127130
LaunchMap pendingLaunches_;
131+
boost::posix_time::time_duration pendingLaunchWindow_ = boost::posix_time::minutes(1);
132+
boost::posix_time::time_duration stalePendingLaunchAge_ = boost::posix_time::minutes(3);
128133

129134
// session launch function
130135
SessionLaunchFunction sessionLaunchFunction_;

‎src/cpp/server/session/ServerSessionManager.cpp‎

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,26 @@ Error SessionManager::launchSession(boost::asio::io_context& ioContext,
270270
LaunchMap::const_iterator pos = pendingLaunches_.find(context);
271271
if (pos != pendingLaunches_.end())
272272
{
273-
// if the launch is less than one minute old then return success
274-
if ( (pos->second.launchTime + boost::posix_time::minutes(1))
275-
> microsec_clock::universal_time() )
273+
// if the launch is still within its window, or its process is still
274+
// alive (a slow-starting session: a duplicate would rebind the same
275+
// session socket and fail on the first session's source database
276+
// locks), then return success
277+
ptime now = microsec_clock::universal_time();
278+
if ( (pos->second.launchTime + pendingLaunchWindow_) > now )
276279
{
277-
LOG_DEBUG_MESSAGE("Found existing recent launch < 1 min for: " + context.username + " id: " + context.scope.id());
280+
LOG_DEBUG_MESSAGE("Found existing recent launch < " + std::to_string(pendingLaunchWindow_.total_seconds()) +
281+
" secs for: " + context.username + " id: " + context.scope.id());
282+
283+
launched = false;
284+
return Success();
285+
}
286+
else if (pos->second.pid != -1 &&
287+
(pos->second.launchTime + stalePendingLaunchAge_) > now &&
288+
core::system::isProcessRunning(pos->second.pid))
289+
{
290+
DLOGF("Found pending launch of live session process {} started {} secs ago for user {} (id: {}) - not relaunching",
291+
pos->second.pid, (now - pos->second.launchTime).total_seconds(),
292+
context.username, context.scope.id());
278293

279294
launched = false;
280295
return Success();
@@ -431,6 +446,17 @@ Error SessionManager::launchAndTrackSession(
431446
return Success();
432447
}
433448

449+
void SessionManager::setPendingLaunchTimeouts(const boost::posix_time::time_duration& launchWindow,
450+
const boost::posix_time::time_duration& staleAge)
451+
{
452+
LOCK_MUTEX(launchesMutex_)
453+
{
454+
pendingLaunchWindow_ = launchWindow;
455+
stalePendingLaunchAge_ = staleAge;
456+
}
457+
END_LOCK_MUTEX
458+
}
459+
434460
void SessionManager::setSessionLaunchFunction(
435461
const SessionLaunchFunction& launchFunction)
436462
{
@@ -462,7 +488,7 @@ void SessionManager::removePendingLaunch(const r_util::SessionContext& context,
462488
// forever, still holding the project and Posit Assistant locks
463489
// (#18572). keep the entry while its process is alive; if it never
464490
// becomes reachable, the exit tracker clears it when it dies and
465-
// launchSession's one-minute window expires it otherwise. custom
491+
// launchSession's stale pending launch age expires it otherwise. custom
466492
// session launchers never record a pid, so they keep the old
467493
// clear-on-error behavior.
468494
if (!success &&
@@ -588,7 +614,7 @@ int SessionManager::cleanStalePendingLaunches()
588614
int numRemoved = 0;
589615
while (it != pendingLaunches_.cend())
590616
{
591-
if (now > (it->second.launchTime + boost::posix_time::minutes(3)))
617+
if (now > (it->second.launchTime + stalePendingLaunchAge_))
592618
{
593619
pendingLaunches_.erase(it++);
594620
numRemoved++;

0 commit comments

Comments
 (0)