Skip to content

fix: second instance return false #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

huanguan1978
Copy link

No description provided.

@arran4 arran4 self-assigned this Mar 29, 2025
@arran4 arran4 self-requested a review March 29, 2025 04:51
@arran4
Copy link
Owner

arran4 commented Mar 29, 2025

Thanks for this @huanguan1978 I will have to review it against my app, and the other "single instance" code bases. Let me know if you need me to hurry on that - I have a couple other things to look into.

@huanguan1978
Copy link
Author

// ---- example from example/unix_single_instance_example.dart

void main(List<String> args) async {
    WidgetsFlutterBinding.ensureInitialized();
    if (Platform.isLinux) {
      if (!await unixSingleInstance(arguments, cmdProcessor)) { // <--  error: this function call exit(0), nothing return.
        exit(0);
        return;
      }
    } else if (Platform.isMacOS) {
      if (!await unixSingleInstance(arguments, cmdProcessor)) {
        exit(0);
        return;
      }
    }
    runApp(const MyApp());
}

// My First Instance

Launching lib/main.dart on macOS in release mode...
Building macOS application...
✓ Built build/macos/Build/Products/Release/filetimes.app (58.9MB)

Flutter run key commands.
h List all available interactive commands.
c Clear the screen
q Quit (terminate the application on the device).
flutter: ---main--- args:, [--action, list, --source, ~/Documents/Projects/avatar/filetimes]
flutter: ---main---, EnvArg.action: list
flutter: ---main---, EnvArg.source: ~/Documents/Projects/avatar/filetimes
flutter: Found existing instance!
flutter: Socket connect error
flutter: SocketException: Connection failed (OS Error: Connection refused, errno = 61), address = /Users/kaguya/Library/Containers/com.iche2.filetimes/Data/Documents/socket, port = 0 
flutter: Deleting dead socket
flutter: creating socket
flutter: creating listening
flutter: Event
flutter: Instance of '_Socket'
flutter: Second instance launched with: ["--action","list","--source","~/Documents/Projects/avatar/filetime"]

flutter: ---cmdProcessor---, decodedArgs.forEach, --action
flutter: ---cmdProcessor---, decodedArgs.forEach, list
flutter: ---cmdProcessor---, decodedArgs.forEach, --source
flutter: ---cmdProcessor---, decodedArgs.forEach, ~/Documents/Projects/avatar/filetime
flutter: ---cmdProcessor---, argsToMap, {action: list, source: ~/Documents/Projects/avatar/filetime}

// --------------------------------------------------------------------------------
// My Second Instance
Launching lib/main.dart on macOS in release mode...
Building macOS application...
✓ Built build/macos/Build/Products/Release/filetimes.app (58.9MB)

Flutter run key commands.
h List all available interactive commands.
c Clear the screen
q Quit (terminate the application on the device).
flutter: ---main--- args:, [--action, list, --source, ~/Documents/Projects/avatar/filetime]
flutter: ---main---, EnvArg.action: list
flutter: ---main---, EnvArg.source: ~/Documents/Projects/avatar/filetime
flutter: Found existing instance!
flutter: Message sent
flutter: Quiting.   // <— fix: Second Instance output message ' Quiting'  and  exit(0), not return false.

@arran4
Copy link
Owner

arran4 commented Mar 30, 2025

Hey @huanguan1978 no debate required. I agree I just need to do due diligence. Although what you provided is actually useful. Thanks.

Do you need this merged and released ASAP or can you wait?

@huanguan1978
Copy link
Author

huanguan1978 commented Mar 30, 2025

Thanks @arran4 , please merge this.

Also, I have a few questions. Could you provide guidance? Based on your logic, upon startup, 'First Instance' checks for the file 'socket'. If it exists, it displays 'Found existing instance!' then deletes and recreates it. Is there a method to reuse this socket, similar to MySQL?"

flutter: Found existing instance!
flutter: Socket connect error
flutter: SocketException: Connection failed (OS Error: Connection refused, errno = 61), address = /Users/kaguya/Library/Containers/com.iche2.filetimes/Data/Documents/socket, port = 0 
flutter: Deleting dead socket
flutter: creating socket
flutter: creating listening

@arran4
Copy link
Owner

arran4 commented Mar 30, 2025

Are you looking to use a custom message? -- What do you want to reuse it for?

@huanguan1978
Copy link
Author

I envision a GUI application running as a single instance, controlled dynamically via command-line arguments. This would enable actions like adding/removing/starting/stopping scheduled tasks, and viewing task logs from within the instance.

The core of this control mechanism should be implemented using Unix sockets.👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants