Repository navigation
Closed
Labels
Milestone
Description
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
wfurt commentedon Mar 29, 2021
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=Cbut the the ping would still fail ifLC_MESSAGE=ja_JP.utf8is set. So I think we would need to sanitize all variables related to localization.wfurt commentedon Mar 29, 2021
https://help.ubuntu.com/community/Locale
iSazonov commentedon Mar 30, 2021
Improving ParseRoundTripTime() would be simpler - find last '=' and parse a decimal after it.
smdn commentedon Mar 31, 2021
@wfurt
According to https://help.ubuntu.com/community/Locale,
LC_ALLoverridesLANGand any otherLC_*envvars.So it seems to be enough to set
LC_ALL=Cinstead of sanitizingLANGand allLC_*.@iSazonov
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_NUMERICcan 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 msforLC_NUMERIC=fr_FR(comma is used as decimal point), we will have to fix ParseRoundTripTime() again.Currently,
ParseRoundTripTime()is doingdouble.ParsewithCultureInfo.InvariantCulture. We will need to change this to useCultureInfo("fr_FR")to parse "0,123" correctly in this case.runtime/src/libraries/Common/src/System/Net/NetworkInformation/UnixCommandLinePing.cs
Lines 197 to 206 in 99d6215
Considering the effect of changing locale or the possibility of localization updates in the future, I think setting
LC_ALL=Cis the stable and better approach.smdn commentedon Mar 31, 2021
Workaround for this issue.
Set LC_ALL=C before running ping process.
Set LC_ALL=C before running ping process in Ping.Send() (#50696)