fix: mutex handling in wholescreencapture
This commit is contained in:
@@ -198,7 +198,7 @@ func (a *appImpl) processScreenshot() {
|
||||
// Capture screenshot
|
||||
filePath, err := a.capturer.Get()
|
||||
if err != nil {
|
||||
a.log.Error("Failed to capture screenshot", "error", err)
|
||||
a.log.Error("failed to obtain screenshot from capturer", "error", err)
|
||||
a.addConsoleOutput("Error: Failed to capture screenshot")
|
||||
a.stats.Status = Waiting
|
||||
return
|
||||
|
||||
@@ -57,7 +57,11 @@ type wholeScreenCapturer struct {
|
||||
|
||||
// Get implements ScreenCapturer.
|
||||
func (w *wholeScreenCapturer) Get() (filepath string, err error) {
|
||||
if !w.initialized {
|
||||
w.mu.RLock()
|
||||
initialized := w.initialized
|
||||
w.mu.RUnlock()
|
||||
|
||||
if !initialized {
|
||||
return "", ErrNotInitialized
|
||||
}
|
||||
|
||||
@@ -73,6 +77,9 @@ func (w *wholeScreenCapturer) Get() (filepath string, err error) {
|
||||
|
||||
// Init implements ScreenCapturer.
|
||||
func (w *wholeScreenCapturer) Init() (err error) {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
|
||||
if w.initialized {
|
||||
w.log.Debug("wholescreencapturer already initialized, skipping initialization")
|
||||
return nil
|
||||
@@ -104,6 +111,7 @@ func (w *wholeScreenCapturer) Init() (err error) {
|
||||
}
|
||||
|
||||
func (w *wholeScreenCapturer) captureAndSave() (string, error) {
|
||||
w.mu.RLock()
|
||||
img, err := screenshot.CaptureRect(w.displayBounds)
|
||||
if err != nil {
|
||||
w.log.Error("failed to capture screenshot", "error", err)
|
||||
@@ -113,6 +121,7 @@ func (w *wholeScreenCapturer) captureAndSave() (string, error) {
|
||||
now := time.Now().UnixMilli()
|
||||
filename := fmt.Sprintf("%d.png", now)
|
||||
filePath := filepath.Join(w.tempDirectory, filename)
|
||||
w.mu.RUnlock()
|
||||
|
||||
file, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
@@ -159,7 +168,11 @@ func NewWholeScreenCapturer(
|
||||
log: log,
|
||||
}
|
||||
lc.Append(fx.StopHook(func(ctx context.Context) error {
|
||||
if !capturer.initialized {
|
||||
capturer.mu.RLock()
|
||||
initialized := capturer.initialized
|
||||
capturer.mu.RUnlock()
|
||||
|
||||
if !initialized {
|
||||
log.Debug("wholescreencapturer not initialized, nothing to do")
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user