Skip to content

Commit

Permalink
Fix single instance lock for macOS sandbox app (#3029)
Browse files Browse the repository at this point in the history
* fix single instance lock for sandbox app

* fix single instance lock for sandbox app
  • Loading branch information
APshenkin authored Nov 5, 2023
1 parent 08e12de commit 426a569
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
2 changes: 2 additions & 0 deletions v2/internal/frontend/desktop/darwin/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ extern void HandleSecondInstanceData(char * message);

void SendDataToFirstInstance(char * singleInstanceUniqueId, char * text);

char* GetMacOsNativeTempDir();

#endif /* AppDelegate_h */
17 changes: 13 additions & 4 deletions v2/internal/frontend/desktop/darwin/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,26 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
}

void SendDataToFirstInstance(char * singleInstanceUniqueId, char * message) {
// we pass message in object because otherwise sandboxing will prevent us from sending it https://developer.apple.com/forums/thread/129437
NSString * myString = [NSString stringWithUTF8String:message];
[[NSDistributedNotificationCenter defaultCenter]
postNotificationName:[NSString stringWithUTF8String:singleInstanceUniqueId]
object:nil
userInfo:@{@"message": [NSString stringWithUTF8String:message]}
object:(__bridge const void *)(myString)
userInfo:nil
deliverImmediately:YES];
}

char* GetMacOsNativeTempDir() {
NSString *tempDir = NSTemporaryDirectory();
char *copy = strdup([tempDir UTF8String]);

return copy;
}

- (void)handleSecondInstanceNotification:(NSNotification *)note;
{
if (note.userInfo[@"message"] != nil) {
NSString *message = note.userInfo[@"message"];
if (note.object != nil) {
NSString * message = (__bridge NSString *)note.object;
const char* utf8Message = message.UTF8String;
HandleSecondInstanceData((char*)utf8Message);
}
Expand Down
19 changes: 18 additions & 1 deletion v2/internal/frontend/desktop/darwin/single_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ package darwin
import "C"
import (
"encoding/json"
"fmt"
"github.com/wailsapp/wails/v2/pkg/options"
"os"
"strings"
"syscall"
"unsafe"
)

func SetupSingleInstance(uniqueID string) {
lockFilePath := os.TempDir()
lockFilePath := getTempDir()
lockFileName := uniqueID + ".lock"
_, err := createLockFile(lockFilePath + "/" + lockFileName)

Expand Down Expand Up @@ -62,14 +65,28 @@ func HandleSecondInstanceData(secondInstanceMessage *C.char) {
func createLockFile(filename string) (*os.File, error) {
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
fmt.Printf("Failed to open lockfile %s: %s", filename, err)
return nil, err
}

err = syscall.Flock(int(file.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
if err != nil {
// Flock failed for some other reason than other instance already lock it. Print it in logs for possible debugging.
if !strings.Contains(err.Error(), "resource temporarily unavailable") {
fmt.Printf("Failed to lock lockfile %s: %s", filename, err)
}
file.Close()
return nil, err
}

return file, nil
}

// If app is sandboxed, golang os.TempDir() will return path that will not be accessible. So use native macOS temp dir function.
func getTempDir() string {
cstring := C.GetMacOsNativeTempDir()
path := C.GoString(cstring)
C.free(unsafe.Pointer(cstring))

return path
}

0 comments on commit 426a569

Please sign in to comment.