Skip to content

Ping.Send() throws PingException on Linux non-English locale #50363

Closed

Description

@smdn

Description

On non-EN locale, Ping.Send() throws PingException.
It seem to fail parsing localized output of ping command.

Repro code:

using System;
using System.Net.NetworkInformation;

using var ping = new Ping();

var reply = ping.Send("192.168.0.1");

Console.WriteLine($"{reply.Status} {reply.Address}, {reply.RoundtripTime}[ms]");

If you change the LANG environment variable, the runtime will output different results.

$ echo $LANG
ja_JP.UTF-8

$ dotnet run
Unhandled exception. System.Net.NetworkInformation.PingException: An exception occurred during a Ping request.
 ---> System.Net.NetworkInformation.PingException: An exception occurred during a Ping request.
   at System.Net.NetworkInformation.Ping.SendWithPingUtility(IPAddress address, Byte[] buffer, Int32 timeout, PingOptions options)
   at System.Net.NetworkInformation.Ping.SendPingCore(IPAddress address, Byte[] buffer, Int32 timeout, PingOptions options)
   at System.Net.NetworkInformation.Ping.Send(IPAddress address, Int32 timeout, Byte[] buffer, PingOptions options)
   --- End of inner exception stack trace ---
   at System.Net.NetworkInformation.Ping.Send(IPAddress address, Int32 timeout, Byte[] buffer, PingOptions options)
   at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
   at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress)
   at <Program>$.<Main>$(String[] args) in test.cs:line 6

$ LANG=C dotnet run
Success 192.168.0.1, 0[ms]

$ LANG=en-US.UTF-8 dotnet run
Success 192.168.0.1, 0[ms]

On LANG=C, gets expected result.
Also on LANG=en-US, gets expected result.
However, as an example on LANG=ja_JP, will cause PingException as above.

Outputs of ping command on each locales are like below.

$ echo $LANG
ja_JP.UTF-8

$ ping 192.168.0.1
PING 192.168.0.1 (192.168.0.1) 56(84) バイトのデータ
64 バイト応答 送信元 192.168.0.1: icmp_seq=1 ttl=64 時間=0.359ミリ秒
64 バイト応答 送信元 192.168.0.1: icmp_seq=2 ttl=64 時間=0.320ミリ秒
^C
--- 192.168.0.1 ping 統計 ---
送信パケット数 2, 受信パケット数 2, パケット損失 0%, 時間 1022ミリ秒
rtt 最小/平均/最大/mdev = 0.320/0.339/0.359/0.019ミリ秒

$ LANG=C ping 192.168.0.1
PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
64 bytes from 192.168.0.1: icmp_seq=1 ttl=64 time=0.428 ms
64 bytes from 192.168.0.1: icmp_seq=2 ttl=64 time=0.393 ms
^C
--- 192.168.0.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1029ms
rtt min/avg/max/mdev = 0.393/0.410/0.428/0.017 ms

Configuration

  • .NET SDK 5.0.201, 6.0.0-preview.2.21154.6
  • Ubuntu 20.04.2 LTS

Other information

I think this issue may be fixed by adding ProcessStartInfo.EnvironmentVariables.Add("LANG", "C") before running ping command.

Activity

  1. ghost added
    untriaged
    on Mar 29, 2021
  2. wfurt commented on Mar 29, 2021

    @wfurt
    Member

    I can reproduces it after also adding localized packages to my system. I think clearing the localization makes sense as general strategy. However, one can for example set LANG=C but the the ping would still fail if LC_MESSAGE=ja_JP.utf8 is set. So I think we would need to sanitize all variables related to localization.

  3. added
    help wanted
    and removed
    untriaged
    on Mar 29, 2021
  4. added this to the 6.0.0 milestone on Mar 29, 2021
  5. iSazonov commented on Mar 30, 2021

    @iSazonov
    Contributor

    So I think we would need to sanitize all variables related to localization.

    Improving ParseRoundTripTime() would be simpler - find last '=' and parse a decimal after it.

  6. smdn commented on Mar 31, 2021

    @smdn
    ContributorAuthor

    @wfurt

    According to https://help.ubuntu.com/community/Locale, LC_ALL overrides LANG and any other LC_* envvars.
    So it seems to be enough to set LC_ALL=C instead of sanitizing LANG and all LC_*.

    $ echo LANG=$LANG LC_ALL=$LC_ALL LC_MESSAGES=$LC_MESSAGES
    LANG=ja_JP.UTF-8 LC_ALL= LC_MESSAGES=
    
    $ LC_ALL=C ping -c 1 192.168.0.1
    PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
    64 bytes from 192.168.0.1: icmp_seq=1 ttl=64 time=0.362 ms
    
    --- 192.168.0.1 ping statistics ---
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 0.362/0.362/0.362/0.000 ms
    
    $ LANG=en_US.UTF8 LC_MESSAGES=ja_JP.UTF8 LC_ALL=C ping -c 1 192.168.0.1
    PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
    64 bytes from 192.168.0.1: icmp_seq=1 ttl=64 time=0.352 ms
    
    --- 192.168.0.1 ping statistics ---
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 0.352/0.352/0.352/0.000 ms
    

    @iSazonov

    Improving ParseRoundTripTime() would be simpler - find last '=' and parse a decimal after it.

    Yes, it's simpler. But I think such approach may cause the similar problem in the future.

    For example, according to the ubuntu document LC_NUMERIC can be set to change the number format.
    If the format is changed in the localization update in the future, and output changes to format like 0,123 ms for LC_NUMERIC=fr_FR(comma is used as decimal point), we will have to fix ParseRoundTripTime() again.

    Currently, ParseRoundTripTime() is doing double.Parse with CultureInfo.InvariantCulture. We will need to change this to use CultureInfo("fr_FR") to parse "0,123" correctly in this case.

    public static long ParseRoundTripTime(string pingOutput)
    {
    int timeIndex = pingOutput.IndexOf("time=", StringComparison.Ordinal);
    int afterTime = timeIndex + "time=".Length;
    int msIndex = pingOutput.IndexOf("ms", afterTime, StringComparison.Ordinal);
    int numLength = msIndex - afterTime - 1;
    ReadOnlySpan<char> timeSubstring = pingOutput.AsSpan(afterTime, numLength);
    double parsedRtt = double.Parse(timeSubstring, provider: CultureInfo.InvariantCulture);
    return (long)Math.Round(parsedRtt);
    }

    Considering the effect of changing locale or the possibility of localization updates in the future, I think setting LC_ALL=C is the stable and better approach.

  7. smdn commented on Mar 31, 2021

    @smdn
    ContributorAuthor

    Workaround for this issue.

    using System;
    using System.Net.NetworkInformation;
    
    using var ping = new Ping();
    
    // retain current value of LC_ALL
    var LC_ALL = Environment.GetEnvironmentVariable("LC_ALL");
    
    try {
      // set LC_ALL=C temporarily before send ping
      Environment.SetEnvironmentVariable("LC_ALL", "C");
    
      var reply = ping.Send("192.168.0.1");
    
      Console.WriteLine($"{reply.Status} {reply.Address}, {reply.RoundtripTime}[ms]");
    }
    finally {
      // revert LC_ALL to previous value
      Environment.SetEnvironmentVariable("LC_ALL", LC_ALL);
    }
    $ LANG=ja_JP.UTF8 dotnet run
    Success 192.168.0.1, 0[ms]
    
    $ LANG=en_US.UTF8 LC_MESSAGES=ja_JP.UTF8 dotnet run
    Success 192.168.0.1, 0[ms]
    
    $ LANG=en_US.UTF8 LC_MESSAGES=en_US.UTF8 dotnet run
    Success 192.168.0.1, 0[ms]
    
  8. added a commit that references this issue on Apr 4, 2021
    7f830e8
  9. ghost added
    in-pr
    on Apr 4, 2021
  10. added a commit that references this issue on Apr 8, 2021
    d927fcb
  11. ghost removed
    in-pr
    on Apr 8, 2021
  12. ghost locked as resolved and limited conversation to collaborators on May 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Nethelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions