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