Skip to content

Tcp Keepalive for Linux (NET8.0) #1120

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 4 commits into
base: master
Choose a base branch
from

Conversation

chrbauer
Copy link

New Target .NET 8.0
Fixes setup of Tcp Keepalive under Linux

New Target .NET 8.0
Fixes setup of Tcp Keepalive under Linux
@chrbauer chrbauer force-pushed the linux/tcp-keepalive-for-net80 branch from f7818c5 to 1c4add8 Compare April 11, 2025 15:44
@AlexandreArpin
Copy link

I think adding targets to all currently supported net targets (so 8/9) would probably be a good thing, it should probably be done in another PR.

I maintain a fork since activity had slowed down on the repository and I wanted to "modernize" the repository a bit, I ended up completing the work of #1073 (and a bunch of smaller open PR) which I think is the right way about adding support to net target.

More precisely, a lot of the changes in this PR use the pragma NET8_0_OR_GREATER when they probably should use NETSTANDARD2_1_OR_GREATER.

It would be very nice to keep some of the momentum going from #1111

@chrbauer
Copy link
Author

Modernizing the project is a great idea. Even net6 had reached eol. The NETSTANDARD2_1_OR_GREATER could be indeed used on many places, but but not everywhere. The Pull request solves the core problem only for net8.

I would like to have this merged. Unsure how to proceed.

@@ -82,7 +82,7 @@ public async void AsyncWithCancellationToken()
await Assert.ThrowsAnyAsync<OperationCanceledException>(async () => await server.ReceiveStringAsync(source.Token));
}

#if NETCOREAPP3_1
#if NET8_0_OR_GREATER

Choose a reason for hiding this comment

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

IAsyncEnumerable earliest's availability is on Net Standard 2.1, I believe this check to be too restrictive in theory (although this is just a test project).

It should probably be either #if NET or #if NETSTANDARD2_1_OR_GREATER?

@@ -1,6 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE.md file in the project root for more information.

#if !NETSTANDARD2_1
#if !NETSTANDARD2_1 && !NET8_0_OR_GREATER

Choose a reason for hiding this comment

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

image

Since this is to "stub in" attributes that only available on later frameworks, we probably should only target the missing frameworks?

Should probably be #if NET_FRAMEWORK?

@@ -1,4 +1,4 @@
#if NETSTANDARD2_0 || NETSTANDARD2_1 || NET47
#if NET8_0_OR_GREATER || NET8_0_OR_GREATER || NETSTANDARD2_0 || NETSTANDARD2_1 || NET47

Choose a reason for hiding this comment

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

NET8_0_OR_GREATER was added twice.

I also believe a more "generic" statement like #if NETSTANDARD2_0_OR_GREATER || NET47_OR_GREATER would target all compatible frameworks and be "future" proof (won't need to review pragma if like more .NetFramework come out)

@@ -157,7 +157,7 @@ PullMsgResult ProduceWelcome(ref Msg msg)
// 8-byte prefix plus 16-byte random nonce
CookieNoncePrefix.CopyTo(cookieNonce);
using var rng = RandomNumberGenerator.Create();
#if NETSTANDARD2_1
#if NET8_0_OR_GREATER || NETSTANDARD2_1

Choose a reason for hiding this comment

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

image

Spans are available in all non-NetFramework and NetStandard2.1, so it should probably be #if NETSTANDARD2_1_OR_GREATER

@@ -248,6 +248,13 @@ public void OutCompleted(SocketError socketError, int bytesTransferred)
// Set the TCP keep-alive option values to the underlying socket.
m_s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, m_options.TcpKeepalive);


#if NET8_0_OR_GREATER

Choose a reason for hiding this comment

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

Last comment in the same "general" idea, these options became available in net5/netcore3.1, might be worth to do a #if NET here?

Copy link
Author

Choose a reason for hiding this comment

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

I used the #if NET idea and made a commit

@AlexandreArpin
Copy link

Modernizing the project is a great idea. Even net6 had reached eol. The NETSTANDARD2_1_OR_GREATER could be indeed used on many places, but but not everywhere. The Pull request solves the core problem only for net8.

I would like to have this merged. Unsure how to proceed.

I'm in favor of adding net8/9 targets in additional to .net standard, although it should probably be done in its own PR and when the targets are added, then rebase this PR to only add the patch for the keep alive in the socket classes?

@chrbauer
Copy link
Author

I will prepare an update to set also TcpKeepaliveCnt...

@chrbauer
Copy link
Author

I'm in favor of adding net8/9 targets in additional to .net standard, although it should probably be done in its own PR and when the targets are added, then rebase this PR to only add the patch for the keep alive in the socket classes?
I would be happy to do the rebase, if such a branch exists.

@chrbauer
Copy link
Author

I will prepare an update to set also TcpKeepaliveCnt...

This was done last week

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