Flush the session snapshot on activity onStop (Android)
The OS provides no user-space hook for a process kill, but the activity onStop fires on every 'going away' transition the framework still controls: entering recents (the swipe-wipe path), app switch, and home. Recents-wipe then kills the process right after onStop returns, so that moment is the last reliable flush. - Logic.FlushSession (any goroutine, buffered, non-blocking) -> flushSessionSave on the owner: persist the snapshot now, bypassing the rate limit (still honoring the restore-pending suppression). saveSessionIfChanged's persist tail is deduplicated into writeSession. - JNI: GioActivity.onStop (patched into the smali by the build scripts, in sync) now calls the static native padFlushSession, which maps to the pad_flush_session cgo export. Verified on emulator: the flush fires on home/app-switch and when the app is backgrounded into recents before a kill; a state change made inside the 250ms rate window and then backed out of the app persists the latest position. A hard kill while foregrounded (force-stop, memory pressure) still has no hook — the immediate edit saves plus the 250ms rate window bound that loss.
This commit is contained in:
@@ -73,6 +73,10 @@ type Logic struct {
|
||||
openFileChan chan string
|
||||
retryChan chan string // auto-save retries
|
||||
autosaveChan chan struct{} // auto-save debounce ticks (timer -> owner)
|
||||
// flushSession: one-shot request to persist the session snapshot now
|
||||
// (the OS activity onStop hook, see FlushSession). Buffered 1 so the
|
||||
// requester never blocks, even if an earlier flush is still queued.
|
||||
flushSession chan struct{}
|
||||
inspectChan chan *inspectReq
|
||||
|
||||
// Per-file write protocol (see requestSave). Workers are a shared pool and
|
||||
@@ -156,6 +160,7 @@ func NewLogic(mfs pool.FileSystem, path string, openfunc func(string)) *Logic {
|
||||
openFileChan: make(chan string),
|
||||
retryChan: make(chan string, 1), // Buffered channel
|
||||
autosaveChan: make(chan struct{}),
|
||||
flushSession: make(chan struct{}, 1),
|
||||
inspectChan: make(chan *inspectReq),
|
||||
writeInFlight: make(map[string]int),
|
||||
savePending: make(map[string]bool),
|
||||
@@ -252,6 +257,11 @@ func (l *Logic) Run() {
|
||||
// the final flush and promote a stale snapshot.
|
||||
l.drainWrites()
|
||||
return
|
||||
case <-l.flushSession:
|
||||
// Persist now, bypassing the rate limit: the activity is going
|
||||
// away (recents-wipe or app switch) and the process may die
|
||||
// shortly after this returns.
|
||||
l.flushSessionSave()
|
||||
case update := <-l.configChan:
|
||||
update.apply(l.state)
|
||||
if _, ok := update.(ScaleEvent); ok {
|
||||
|
||||
@@ -120,11 +120,46 @@ func (l *Logic) saveSessionIfChanged() {
|
||||
if !urgent && now.Sub(l.lastSessionSave) < sessionSaveInterval {
|
||||
return
|
||||
}
|
||||
l.writeSession(s, now)
|
||||
}
|
||||
|
||||
// flushSessionSave persists the current snapshot now, bypassing the rate
|
||||
// limit (still honoring the restore-pending suppression: a pre-land
|
||||
// snapshot would clobber the positions being restored). Must be called on
|
||||
// the logic goroutine.
|
||||
func (l *Logic) flushSessionSave() {
|
||||
if l.sessionSaver == nil || l.session.File != "" {
|
||||
return
|
||||
}
|
||||
s := l.SnapshotSession()
|
||||
if s == l.lastSession {
|
||||
return
|
||||
}
|
||||
l.writeSession(s, time.Now())
|
||||
}
|
||||
|
||||
// writeSession is the shared persist tail: hand the snapshot to the cmd
|
||||
// layer's saver and record it as the new baseline. Must be called on the
|
||||
// logic goroutine.
|
||||
func (l *Logic) writeSession(s SessionState, now time.Time) {
|
||||
l.sessionSaver(s)
|
||||
l.lastSession = s
|
||||
l.lastSessionSave = now
|
||||
}
|
||||
|
||||
// FlushSession requests an immediate session persist from any goroutine.
|
||||
// The Android activity's onStop (recents-wipe, app switch) calls it via
|
||||
// JNI: the OS gives no user-space hook for the subsequent process kill,
|
||||
// so onStop is the last reliable moment to flush. Buffered delivery means
|
||||
// the caller never blocks; if an earlier flush is still queued, one
|
||||
// snapshot is coalesced into it (the state is the same frame's anyway).
|
||||
func (l *Logic) FlushSession() {
|
||||
select {
|
||||
case l.flushSession <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
// BeginRestore prepares the state for relaunch restoration: the last file
|
||||
// is re-opened and the editor page shown immediately. The cursor,
|
||||
// selection, find state and scroll land as the file's stat/content arrive
|
||||
|
||||
Reference in New Issue
Block a user