Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(cmd, osutil): implement Windows Services API communication (fixes #7042)#10524

Open
rafaeloledo wants to merge 4 commits intosyncthing:mainfrom
rafaeloledo:main
Open

feat(cmd, osutil): implement Windows Services API communication (fixes #7042)#10524
rafaeloledo wants to merge 4 commits intosyncthing:mainfrom
rafaeloledo:main

Conversation

@rafaeloledo
Copy link

@rafaeloledo rafaeloledo commented Jan 10, 2026

Purpose

The purpose of this change is related to syncthing/docs#955. Since --no-console is being discouraged on Windows 11 due to Windows Terminal being the new default "console".

Here, i'm adding support for the Windows Services API in the project. It can now communicate and act as a service for Windows Services Manager.

It's necessary to the service be able to Start and Stop on the Manager's GUI.

2026-01-10.03-30-33.mp4

Currently, the Stop action is working in functionality, but it can be coded better.

Testing

Run the following commands on Windows:

Build the app with fake version

go run build.go --version v2.0.14

Then, create a service linked to the syncthing.exe path.

sc create syncthing binPath="C:\repos\syncthing\bin\syncthing.exe --no-browser" start=auto DisplayName="syncthing"

Run the service

sc start syncthing

Stop the service

sc stop syncthing

Delete the service

sc stop syncthing        (if running)
sc delete syncthing

https://learn.microsoft.com/en-us/windows/win32/services/services

Authorship

Name: rafaeloledo
Email: [email protected]

@github-actions github-actions bot added the enhancement New features or improvements of some kind, as opposed to a problem (bug) label Jan 10, 2026
s <- c.CurrentStatus
case svc.Stop, svc.Shutdown:
s <- svc.Status{State: svc.StopPending}
_ = exec.Command("taskkill", "/IM", "syncthing.exe", "/F").Run()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to stop in such a reflective manner? This should simply result in a regular shutdown from within.

@rafaeloledo rafaeloledo force-pushed the main branch 2 times, most recently from 7a5b1bd to caf26a5 Compare January 10, 2026 18:56
Interrupt works well when the service is on console emulating CTRL+C/CTRL+D signals

in this case, it's working as a service tied to Windows Service API, so it's not working

executing a manual separate taskkill is discouraged

also remove code that is not needed

Signed-off-by: rafaeloledo <[email protected]>
Co-authored-by: bt90 <[email protected]>
@rafaeloledo
Copy link
Author

rafaeloledo commented Jan 10, 2026

@bt90 So, i was trying to implement a good solution for stopping the process tree. The most recommended is sending Interrupt signal. Unfortunately, it's not working on my tests.

Sending kill to the process is accomplishing the goal of the "Stop" action. The problem is shown in the video above. Although it finishes, Windows gives a warning.

If i stop the service in the CLI, no warnings are given. So, the warning is a non-blocking action. It's better than no communication.

2026-01-10.19-06-52.mp4

If anyone knows how to improve the Stop action, i can merge it here.

@calmh
Copy link
Member

calmh commented Jan 11, 2026

Isn't the thing that the notification is sent to the running process, so you don't need to find a process and send it a signal, because it is ourselves... We need to cancel the context in main so that the process exits?

@rafaeloledo rafaeloledo force-pushed the main branch 2 times, most recently from 6ba70e5 to 85521a0 Compare January 11, 2026 22:18
@rafaeloledo
Copy link
Author

rafaeloledo commented Jan 11, 2026

Seems alright now

2026-01-11.19-22-39.mp4

IMHO, this changes doesn't affect the current behavior of syncthing and are complementary.

Running as a normal binary (not Windows Service):
image

Signed-off-by: rafaeloledo <[email protected]>
Co-authored-by: Jakob Borg <[email protected]>
@tomasz1986
Copy link
Member

Does the PR fix #7042?

@rafaeloledo
Copy link
Author

rafaeloledo commented Jan 11, 2026

Does the PR fix #7042?

I think so. Just to remind, i've implemented Start and Stop actions. It can start with windows without popping the terminal whatsoever.

Btw, this issue is been there for a while...

2020 👀.

I don't know if my implementation is 100%, so, i'm accepting any improvements.

@tomasz1986 tomasz1986 changed the title feat: implement Windows Services API communication feat(cmd, osutil): implement Windows Services API communication (fixes #7042) Jan 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New features or improvements of some kind, as opposed to a problem (bug)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants