"ifdown wlanx" make system hang


I am using BB Black RevC. It is offered by Element 14; its OS is Debian.
The BBB is used to collect some sensor data and forward it to a server through WiFi interface; the WiFi dongle used is TP-Link TL-WN821N.
The BBB is required to function at 7/24 basis. So I run a script inside BBB to do check and maintain WiFi connection:

while (true) {
res = fping router a few times
if (res == yes) {

do nothing

} else {

ifdown wlanx
ifup wlanx

However, it happens quite often during my test that ifdown wlanx will make the BBB hang.
Anyone has similar issue? Is it hardware related: the TP-Link dongle and its driver; Or is there some bug in OS?

Same thing happens to me every time.
Using a REALTEK wifi dongle

don’t use DHCP.
I change wlanx setting to static IP, the problem never happen.