From d1b699368466f3da5811bec021bec03fe5578ab2 Mon Sep 17 00:00:00 2001 From: "zhengyu.zhan" Date: Mon, 23 May 2022 09:54:01 +0800 Subject: [PATCH] Fix tethering WTF issue When enable/disable tethering quickly, tethering may have stopped when Ipserver start creatFd(). As tethering iface is down due to tethering stopped, creatFd() will throw ErrnoException: bind failed: ENODEV (No such device). When tethering is stopped, this exception is normal, should not rise WTF. Bug: 233587050 Test: manual test Change-Id: I2e200313d1bec2c1e1245e779f3875397127160d --- .../src/android/net/ip/NeighborPacketForwarder.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Tethering/src/android/net/ip/NeighborPacketForwarder.java b/Tethering/src/android/net/ip/NeighborPacketForwarder.java index 723bd63543..8384562762 100644 --- a/Tethering/src/android/net/ip/NeighborPacketForwarder.java +++ b/Tethering/src/android/net/ip/NeighborPacketForwarder.java @@ -23,6 +23,7 @@ import static android.system.OsConstants.IPPROTO_RAW; import static android.system.OsConstants.SOCK_DGRAM; import static android.system.OsConstants.SOCK_NONBLOCK; import static android.system.OsConstants.SOCK_RAW; +import static android.system.OsConstants.ENODEV; import android.net.util.SocketUtils; import android.os.Handler; @@ -131,7 +132,13 @@ public class NeighborPacketForwarder extends PacketReader { ETH_P_IPV6, mListenIfaceParams.index); Os.bind(mFd, bindAddress); } catch (ErrnoException | SocketException e) { - Log.wtf(mTag, "Failed to create socket", e); + // An ENODEV(No such device) will rise if tethering stopped before this function, this + // may happen when enable/disable tethering quickly. + if (e instanceof ErrnoException && ((ErrnoException) e).errno == ENODEV) { + Log.w(mTag, "Failed to create socket because tethered interface is gone", e); + } else { + Log.wtf(mTag, "Failed to create socket", e); + } closeSocketQuietly(mFd); return null; }