Server.CreateOwnedAsync
Starts a server and takes ownership of it.
Source and package
- Qualified name
-
LibTmux .Server. Create Owned Async - Namespace
- LibTmux
- Declared in
Server- Package
- LibTmux
- Source
- src/LibTmux/Server.Lifecycle.cs
- static async Task<Owned
Server > CreateOwnedAsync( ServerScope Connection ? options = null, CancellationOptions Token cancellationToken = default ) -
Description
The scope holds an endpoint rather than a materialized server, because a tmux server with no sessions exits at once.
Valuestays that endpoint after a session is created through it; its session, window, and pane listings andCaptureSnapshotAsync()discover the live server on each call, and the objects they return carry the discovered handle.On the socket tmux picks when a caller names none, the server in
TMUXor else the default socket, a server already listening is refused rather than adopted: disposing the scope stops the server, and on a developer's machine that socket holds the one they are using. A named socket or path stays the caller's to adopt deliberately. The check and the start are two tmux calls, so two callers that own the default socket at the same moment can both pass it; give each owned server a socket of its own.A call that fails or is cancelled after starting a server stops it, since no scope reaches the caller to do so; a server it found running is left.
- Parameters
-
-
options(ServerConnection ?) – Connection options.Options Default
null -
cancellationToken(CancellationToken ) – Cancels the tmux commands.Default
default
-
- Returns
-
A scope that stops the server when disposed.
- Errors
-
-
Invalid
Operation – A server is already listening on the default socket.Exception -
Tmux
Command – tmux failed to say whether a server is listening, such as on a socket it may not open.Exception
-
Examples
Distinguish an endpoint handle from a discovered server. Connection discovery returns an immutable replacement without capturing child relations. Use Linux or macOS with tmux 3.3a or newer, the .NET 10 SDK or newer, and the .NET 8 and 10 runtimes. In an empty directory, save this project as
Example.csproj. It references the 0.1.1 release ofLibTmuxon NuGet: View sourceExample.csproj <Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><OutputType>Exe</OutputType><TargetFrameworks>net8.0;net10.0</TargetFrameworks><LangVersion>12.0</LangVersion><Nullable>enable</Nullable><ImplicitUsings>disable</ImplicitUsings><EnableDefaultCompileItems>false</EnableDefaultCompileItems><TreatWarningsAsErrors>true</TreatWarningsAsErrors><AnalysisLevel>10-recommended</AnalysisLevel></PropertyGroup><ItemGroup><Compile Include="Program.cs" /><PackageReference Include="LibTmux" Version="[0.1.1]" /></ItemGroup></Project>Save this complete program as
Program.cs. It creates a private server;await usingdisposes its owned scopes after success, cancellation or failure. The complete file contains explicit imports, a top-level async entry point and a cancellation deadline. View sourceProgram.csComplete program using System;using System.IO;using System.Threading;using LibTmux;if (OperatingSystem.IsWindows()){throw new PlatformNotSupportedException("This example requires tmux on Linux or macOS.");}using CancellationTokenSource deadline = new(TimeSpan.FromSeconds(10));CancellationToken token = deadline.Token;string? testTmux = Environment.GetEnvironmentVariable("LIBTMUX_TEST_TMUX");string? configured = !string.IsNullOrWhiteSpace(testTmux)? testTmux: Environment.GetEnvironmentVariable("LIBTMUX_TMUX");string binary = string.IsNullOrWhiteSpace(configured) ? "tmux" : configured;using TemporaryDirectory socketRoot = new();ServerConnectionOptions options = new(){SocketPath = Path.Combine(socketRoot.FullName, "tmux.sock"),ConfigurationFile = "/dev/null",TmuxBinaryPath = binary,};// Open records the endpoint without running tmux or reading its state.Server endpoint = Server.Open(options);if (endpoint.IsMaterialized){throw new InvalidOperationException("Open must not discover a server.");}await using OwnedServerScope owned = await Server.CreateOwnedAsync(options, token);await using OwnedSessionScope session = await owned.Value.CreateOwnedSessionAsync(new NewSessionRequest { Name = "demo", Command = "/bin/cat" }, token);// Connect discovers the live daemon and returns a new materialized handle.Server connected = await endpoint.ConnectAsync(token);Server sameEndpoint = await Server.ConnectAsync(options, token);if (!connected.IsMaterialized || !sameEndpoint.IsMaterialized || endpoint.IsMaterialized){throw new InvalidOperationException("Connecting must leave the original handle unchanged.");}if (connected.Generation != sameEndpoint.Generation || connected.Sessions.IsCaptured){throw new InvalidOperationException("Connection discovery must not capture the hierarchy.");}Console.WriteLine("Open records an endpoint; Connect discovers the running server.");Console.WriteLine("The original handle stays unmaterialized; relations remain uncaptured.");internal sealed class TemporaryDirectory : IDisposable{private readonly DirectoryInfo _directory = Directory.CreateTempSubdirectory("libtmux-example-");public string FullName => _directory.FullName;public void Dispose() => _directory.Delete(recursive: true);}Build the project and run both supported frameworks. Each program checks its result and exits unsuccessfully after an unhandled error:
Terminal window $ dotnet build Example.csproj --configuration Release && \dotnet bin/Release/net8.0/Example.dll && \dotnet bin/Release/net10.0/Example.dllExpected program output from each framework:
Open records an endpoint; Connect discovers the running server.The original handle stays unmaterialized; relations remain uncaptured.
Recorded calls
source callsThe C# reference records declarations, not calls, so this page shows no callers or callees. That is a limit of the extracted data, not a statement that nothing calls LibTmux.Server.CreateOwnedAsync.
tmux command
task materialThe tmux commands behind CreateOwnedAsync.
tmux C API
task materialThe C declarations in tmux that implement these commands.
Guides
task materialPages that discuss CreateOwnedAsync.
- Guides