fix(server): return correct scanType in startScan response (#5159)

* fix(api): return correct scanType in startScan response

The startScan endpoint launches the scan in a goroutine and immediately
calls GetScanStatus to build the response. Because the scanner hasn't
had time to initialize and write its state to the database, the response
contained stale data from the previous scan (e.g., scanType "quick"
when fullScan=true was requested).

Add a polling loop that waits briefly (up to 3s, polling every 50ms) for
the scanner to report Scanning=true before returning the status. If the
timeout expires, it falls back to the current behavior (no regression).

Fixes #5158

* fix(api): use ticker/timer with context cancellation for scan polling

Replace time.Sleep loop with proper ticker, timer, and ctx.Done()
handling so the poll exits cleanly on timeout or client disconnect.

* fix(api): handle fast scan completion in poll loop

Add a channel to detect when the scan goroutine finishes before the
poll loop observes Scanning=true, avoiding a 3s timeout on very fast
scans. Use defer close to handle both success and error paths.
This commit is contained in:
Deluan Quintão
2026-03-09 14:19:53 -04:00
committed by GitHub
parent e08d4bef16
commit d4b2499e1e
3 changed files with 115 additions and 0 deletions
+23
View File
@@ -14,6 +14,7 @@ type MockScanner struct {
scanFoldersCalls []ScanFoldersCall
scanningStatus bool
statusResponse *model.ScannerStatus
scanStatusFunc func(fullScan bool, targets []model.ScanTarget) *model.ScannerStatus
}
type ScanAllCall struct {
@@ -38,6 +39,13 @@ func (m *MockScanner) ScanAll(_ context.Context, fullScan bool) ([]string, error
m.scanAllCalls = append(m.scanAllCalls, ScanAllCall{FullScan: fullScan})
// Simulate the scanner updating its status when the scan starts
if m.scanStatusFunc != nil {
m.statusResponse = m.scanStatusFunc(fullScan, nil)
} else {
m.scanningStatus = true
}
return nil, nil
}
@@ -54,6 +62,13 @@ func (m *MockScanner) ScanFolders(_ context.Context, fullScan bool, targets []mo
Targets: targetsCopy,
})
// Simulate the scanner updating its status when the scan starts
if m.scanStatusFunc != nil {
m.statusResponse = m.scanStatusFunc(fullScan, targetsCopy)
} else {
m.scanningStatus = true
}
return nil, nil
}
@@ -118,3 +133,11 @@ func (m *MockScanner) SetStatusResponse(status *model.ScannerStatus) {
defer m.mu.Unlock()
m.statusResponse = status
}
// SetScanStatusFunc sets a function that will be called when ScanAll/ScanFolders is invoked,
// simulating the scanner updating its status when the scan starts.
func (m *MockScanner) SetScanStatusFunc(fn func(fullScan bool, targets []model.ScanTarget) *model.ScannerStatus) {
m.mu.Lock()
defer m.mu.Unlock()
m.scanStatusFunc = fn
}