You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
No question about saving the code and session when exiting the program #18818
RStudio Edition : Desktop
RStudio Version : RStudio 2026.09.0+174
OS Version : Windows 10
R Version : 4.6.1.
Steps to reproduce the problem
Open RStudio
Click the X on the top right to close RStudio
Select Cancel (e.g. because you clicked accidentally or changed your mind).
Enter a command in the file content, e.g. a<-2, then execute command.
Click the X at the top right again to close RStudio
Rstudio has been closed, although the content of the open file and the content of the session have changed (new variable) nad not saved..
Describe the problem in detail
In my opinion, after changing the content of the file (RStudio sees this, because the next time you open RStudio, the file has a red title as changed and not saved) or after executing any fragment of code, when you try to close RStudio again, there should be a question about saving. It happened to me that I canceled the closing in the morning, worked all day, and in the afternoon I was not asked to confirm the session/file save before closing. This phenomenon has been occurring in RStudio for at least several years.
Thanks for the clear reproduction steps — they pin this down exactly. This is a bug.
When you close the window, the desktop shell asks the IDE to begin its quit sequence and then immediately marks the quit as confirmed, without waiting to see whether you actually confirmed it. Cancelling the prompt leaves that flag set for the rest of the session, so every later click of the window's close button skips the quit sequence entirely — no prompt for unsaved documents, no prompt to save the workspace. The flag is only cleared when the R session restarts, which is why it persisted from your morning cancel all the way to the afternoon.
Two things that may help in the meantime:
Session > Quit Session (and Session > Restart R) are not affected — they always prompt correctly, even after a cancelled close. Restarting R also clears the stuck state, so the window close button starts prompting again.
Your editor content is not actually lost. RStudio continuously autosaves unsaved editor changes to an internal source database, which is why the file comes back intact and still marked as unsaved the next time you start. That part is working as designed.
What is genuinely lost in this scenario is the R workspace — objects created during the session are not written to .RData, because the quit sequence that would write them never runs.
This affects RStudio Desktop only. On RStudio Server, closing the browser tab isn't a quit at all: the session stays alive and later suspends, preserving your environment, so there's no prompt to miss.
We'll get this fixed.
Implementation notes (for whoever picks this up)
Root cause.MainWindow.closeEvent() in src/node/desktop/src/main/main-window.ts:
if(this.quitConfirmed||!this.sessionProcess||this.sessionProcess.exitCode!==null){closeAllSatellites(this.window);return;// no preventDefault -> window just closes}
...
event.preventDefault();this.executeJavaScript('window.desktopHooks.quitR()').then(()=>(this.quitConfirmed=true))
desktopHooks.quitR() (DesktopHooks.java) is void and just calls commands_.quitSession().execute(), which is asynchronous — it puts up the save prompt and returns. So the executeJavaScript promise resolves as soon as the call returns, before the user answers anything, and quitConfirmed is set unconditionally. Cancelling in ApplicationQuit.prepareForQuit() never notifies the main process. The only reset is in onWorkbenchInitialized(), i.e. session start/reload — hence the flag stays stuck for the life of the session.
Why the flag exists. It lets the programmatic close through without re-intercepting it. But the confirmed-quit path already sets it independently: GWT calls setPendingQuit(PENDING_QUIT_AND_EXIT) then server_.quitSession(); the session exits; SessionLauncher.onRSessionExited() sees PendingQuitAndExit and calls mainWindow.quit(), which sets quitConfirmed = true before window.close(). So the assignment in closeEvent() is redundant on the happy path and is purely the source of this bug.
Suggested fix. Drop the .then(() => (this.quitConfirmed = true)) and let quit() be the sole setter. Before adopting that, confirm every close-after-confirmed-quit route reaches quit() — SessionLauncher.onLaunchError() closes the window directly, but that path is already covered by the !this.sessionProcess || exitCode !== null guard. An alternative, if a signal is wanted rather than a removal, is to make the cancel path in ApplicationQuit call back into the frame so the main process learns the quit was abandoned; the removal is smaller and doesn't add a new cross-boundary call.
Scope. Electron desktop only. quitConfirmed has no server-side counterpart, and the shared GWT quit path leaves no sticky state on cancel: isQuitting_ is set only after confirmation and cleared on failure, JobManager.promptForTermination() recomputes each call, and terminalHelper_.warnBeforeClosing() is re-evaluated per call. There is no main-window close prompt in server mode at all.
Regression test.src/node/desktop/test/unit/main/main-window.test.ts already exercises closeEvent() with a fake for the crashed-renderer case (#18391); add a case in the same style asserting that a closeEvent whose quitR() resolves leaves quitConfirmed false, and that a second closeEvent still calls preventDefault().
Unrelated, noticed in passing.handlingUnsavedChanges_ / isHandlingUnsavedChanges() in ApplicationQuit.java is dead code — never assigned, never called.
Like quitConfirmed, that flag was set from a close the user might still cancel, and it was only cleared on session restart. So in exactly the scenario reported here — cancel the close in the morning, work all day — any window move, resize, or maximize after the cancel was discarded, and the bounds written at quit were the ones from the moment of the cancelled close.
The bounds are now saved in the branch that actually lets the window close, which is the only point at which the geometry is final, and the flag is gone. One behavior change to note: a cancelled close no longer persists geometry at all, so a session that is cancelled and then force-killed keeps its previous bounds instead of the cancel-time ones.
To see it before the fix: cancel a close, move and resize the window, then quit normally and restart — the window returns to where it sat when the close was cancelled.
Activity
gtritchie commentedon Sep 14, 2026
Thanks for the clear reproduction steps — they pin this down exactly. This is a bug.
When you close the window, the desktop shell asks the IDE to begin its quit sequence and then immediately marks the quit as confirmed, without waiting to see whether you actually confirmed it. Cancelling the prompt leaves that flag set for the rest of the session, so every later click of the window's close button skips the quit sequence entirely — no prompt for unsaved documents, no prompt to save the workspace. The flag is only cleared when the R session restarts, which is why it persisted from your morning cancel all the way to the afternoon.
Two things that may help in the meantime:
What is genuinely lost in this scenario is the R workspace — objects created during the session are not written to
.RData, because the quit sequence that would write them never runs.This affects RStudio Desktop only. On RStudio Server, closing the browser tab isn't a quit at all: the session stays alive and later suspends, preserving your environment, so there's no prompt to miss.
We'll get this fixed.
Implementation notes (for whoever picks this up)
Root cause.
MainWindow.closeEvent()insrc/node/desktop/src/main/main-window.ts:desktopHooks.quitR()(DesktopHooks.java) isvoidand just callscommands_.quitSession().execute(), which is asynchronous — it puts up the save prompt and returns. So theexecuteJavaScriptpromise resolves as soon as the call returns, before the user answers anything, andquitConfirmedis set unconditionally. Cancelling inApplicationQuit.prepareForQuit()never notifies the main process. The only reset is inonWorkbenchInitialized(), i.e. session start/reload — hence the flag stays stuck for the life of the session.Why the flag exists. It lets the programmatic close through without re-intercepting it. But the confirmed-quit path already sets it independently: GWT calls
setPendingQuit(PENDING_QUIT_AND_EXIT)thenserver_.quitSession(); the session exits;SessionLauncher.onRSessionExited()seesPendingQuitAndExitand callsmainWindow.quit(), which setsquitConfirmed = truebeforewindow.close(). So the assignment incloseEvent()is redundant on the happy path and is purely the source of this bug.Suggested fix. Drop the
.then(() => (this.quitConfirmed = true))and letquit()be the sole setter. Before adopting that, confirm every close-after-confirmed-quit route reachesquit()—SessionLauncher.onLaunchError()closes the window directly, but that path is already covered by the!this.sessionProcess || exitCode !== nullguard. An alternative, if a signal is wanted rather than a removal, is to make the cancel path inApplicationQuitcall back into the frame so the main process learns the quit was abandoned; the removal is smaller and doesn't add a new cross-boundary call.Scope. Electron desktop only.
quitConfirmedhas no server-side counterpart, and the shared GWT quit path leaves no sticky state on cancel:isQuitting_is set only after confirmation and cleared on failure,JobManager.promptForTermination()recomputes each call, andterminalHelper_.warnBeforeClosing()is re-evaluated per call. There is no main-window close prompt in server mode at all.Regression test.
src/node/desktop/test/unit/main/main-window.test.tsalready exercisescloseEvent()with a fake for the crashed-renderer case (#18391); add a case in the same style asserting that acloseEventwhosequitR()resolves leavesquitConfirmedfalse, and that a secondcloseEventstill callspreventDefault().Unrelated, noticed in passing.
handlingUnsavedChanges_/isHandlingUnsavedChanges()inApplicationQuit.javais dead code — never assigned, never called.Document why a re-dispatched quit sequence is intended
Save window geometry from the close that actually happens
gtritchie commentedon Sep 14, 2026
One more thing turned up while fixing this, in the same function and with the same shape, so it is fixed in the same PR (#18820).
MainWindow.closeEvent()saved the window's position and size on the first close attempt and set ageometrySavedflag so later attempts skipped it:Like
quitConfirmed, that flag was set from a close the user might still cancel, and it was only cleared on session restart. So in exactly the scenario reported here — cancel the close in the morning, work all day — any window move, resize, or maximize after the cancel was discarded, and the bounds written at quit were the ones from the moment of the cancelled close.The bounds are now saved in the branch that actually lets the window close, which is the only point at which the geometry is final, and the flag is gone. One behavior change to note: a cancelled close no longer persists geometry at all, so a session that is cancelled and then force-killed keeps its previous bounds instead of the cancel-time ones.
To see it before the fix: cancel a close, move and resize the window, then quit normally and restart — the window returns to where it sat when the close was cancelled.
Prompt to save on window close after a cancelled quit (#18820)