### Motivation `ConcurrencyHelpers/Lock` has a pthread fallback branch (taken on every non-Windows / non-Darwin platform) that types `LockPrimitive` as `pthread_mutex_t` and initializes `pthread_mutexattr_t` with a default initializer. Both compile on Linux but fail on FreeBSD / OpenBSD, where those types are opaque-pointer typedefs Swift imports as `Optional<OpaquePointer>` — the no-argument initializers don't exist. This blocks swift-service-lifecycle (and everything that depends on it — Hummingbird, downstream HTTP servers) from compiling on FreeBSD with the official Swift FreeBSD preview toolchain. ### Modifications Mirror the pattern landed in [apple/swift-log#387][log-pr]; same change is being submitted for the sibling NIOLock copies (e.g. [apple/swift-http-types#123][sht-pr]): - On `os(FreeBSD) || os(OpenBSD)` alias `LockPrimitive` to `pthread_mutex_t?`. - In `LockOperations.create(_:)`, wrap `pthread_mutexattr_t()` with `#if os(FreeBSD) || os(OpenBSD)` to use the `bitPattern:` overload. [log-pr]: https://github.com/apple/swift-log/pull/387 [sht-pr]: https://github.com/apple/swift-http-types/pull/123 Touches one file (`Sources/ConcurrencyHelpers/Lock.swift`), +7 lines, all behind FreeBSD/OpenBSD conditionals. No behaviour change on existing platforms. ### Result `swift build` and `swift test` both clean on FreeBSD 15.0 with the official Swift FreeBSD preview toolchain: ``` $ uname -a FreeBSD swiftbuild 15.0-RELEASE FreeBSD 15.0-RELEASE-p9 releng/15.0-n281048-6d536196f1bd GENERIC amd64 $ swift --version Swift version 6.3-dev (LLVM 972b62858355d07, Swift 3f8c798cfe25bbb) Target: x86_64-unknown-freebsd14.3 Build config: +assertions $ swift build Building for debugging... ... 276 modules ... Build complete! (42.62s) $ swift test ... ClosureServiceTests passed ... ✔ Test run passed. ``` Without this change `ServiceLifecycle` fails to compile on the same toolchain. ### Checks - [x] Builds on Linux/Darwin (no change there, all changes guarded behind `#if os(FreeBSD) || os(OpenBSD)`). - [x] Builds + passes existing tests on FreeBSD 15.0 with Swift 6.3-dev preview toolchain. - [x] Same pattern as [apple/swift-log#387][log-pr] / [apple/swift-http-types#123][sht-pr].
Swift Service Lifecycle
Swift Service Lifecycle provides a basic mechanism to cleanly start up and shut down an application, freeing resources in-order before exiting.
It also provides a Signal-based shutdown hook, to shut down on signals like TERM or INT.
Swift Service Lifecycle was designed with the idea that every application has some startup and shutdown workflow-like-logic which is often sensitive to failure and hard to get right. The library encodes this common need in a safe and reusable way that is non-framework specific, and designed to be integrated with any server framework or directly in an application. Furthermore, it integrates natively with Structured Concurrency.
This is the beginning of a community-driven open-source project actively seeking contributions, be it code, documentation, or ideas. What Swift Service Lifecycle provides today is covered in the API docs, but it will continue to evolve with community input.
Getting started
Swift Service Lifecycle should be used if you have a server-side Swift application or a cross-platform (e.g. Linux, macOS) application, and you would like to manage its startup and shutdown lifecycle. Below you will find all you need to know to get started.
Adding the dependency
To add a dependency on the package, declare it in your Package.swift:
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.0.0"),
and add ServiceLifecycle to the dependencies of your application target:
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle")
Example Package.swift file with ServiceLifecycle as a dependency:
// swift-tools-version:6.0
import PackageDescription
let package = Package(
name: "my-application",
dependencies: [
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.3.0"),
],
targets: [
.target(name: "MyApplication", dependencies: [
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle")
]),
.testTarget(name: "MyApplicationTests", dependencies: [
.target(name: "MyApplication"),
]),
]
)
Using ServiceLifecycle
You can find a short usage example below. You can find more detailed documentation on how to use ServiceLifecycle here.
ServiceLifecycle consists of two main building blocks. The Service protocol and the ServiceGroup
actor. As a library or application developer you should model your long-running work as services
that implement the Service protocol. The protocol only requires the implementation of a single
func run() async throws method. Once implemented, your application you can use the ServiceGroup
to orchestrate multiple services. The group will spawn a child task for each service and call the
respective run method in the child task. Furthermore, the group will setup signal listeners for
the configured signals and trigger a graceful shutdown on each service.
import ServiceLifecycle
import Logging
// A service can be implemented by a struct, class or actor. For this example we are using a struct.
struct FooService: Service {
func run() async throws {
print("FooService starting")
try await Task.sleep(for: .seconds(10))
print("FooService done")
}
}
@main
struct Application {
static let logger = Logger(label: "Application")
static func main() async throws {
let service1 = FooService()
let service2 = FooService()
let serviceGroup = ServiceGroup(
services: [service1, service2],
gracefulShutdownSignals: [.sigterm],
logger: logger
)
try await serviceGroup.run()
}
}
Security
For the security process, see https://github.com/apple/swift-log?tab=security-ov-file.