Asyncio close connection leave connection in TIME_WAIT state The Next CEO of Stack OverflowWhy is “except: pass” a bad programming practice?Limited concurrency with asyncioasyncio CancelledError and KeyboardInterruptWhat does “connection reset by peer” mean?What is the cost of many TIME_WAIT on the server side?How can I connect to Android with ADB over TCP?No TIME_WAIT connections, why?Too many connections to memcached in TIME_WAIT stateHttpListener leaves connections in TIME_WAITPyQt5 and asyncio: yield from never finishesAsyncio decode utf-8 with StreamReaderWhy TIME_WAIT state is needed by active close side, but not the passive close side?TypeError: _request() got an unexpected keyword argument 'cookies' (aiohttp)
Why do professional authors make "consistency" mistakes? And how to avoid them?
Can I equip Skullclamp on a creature I am sacrificing?
Only print output after finding pattern
What is the point of a new vote on May's deal when the indicative votes suggest she will not win?
Is it my responsibility to learn a new technology in my own time my employer wants to implement?
Too much space between section and text in a twocolumn document
Whats the best way to handle refactoring a big file?
How do we know the LHC results are robust?
How to make a software documentation "officially" citable?
What is the difference between "behavior" and "behaviour"?
How to make a variable always equal to the result of some calculations?
Horror movie/show or scene where a horse creature opens its mouth really wide and devours a man in a stables
How to count occurrences of text in a file?
Increase performance creating Mandelbrot set in python
Is HostGator storing my password in plaintext?
Where to find order of arguments for default functions
How should I support this large drywall patch?
Why here is plural "We went to the movies last night."
Grabbing quick drinks
How do I go from 300 unfinished/half written blog posts, to published posts?
What happens if you roll doubles 3 times then land on "Go to jail?"
Putting a 2D region plot under a 3D plot
Why is there a PLL in CPU?
Can a caster that cast Polymorph on themselves stop concentrating at any point even if their Int is low?
Asyncio close connection leave connection in TIME_WAIT state
The Next CEO of Stack OverflowWhy is “except: pass” a bad programming practice?Limited concurrency with asyncioasyncio CancelledError and KeyboardInterruptWhat does “connection reset by peer” mean?What is the cost of many TIME_WAIT on the server side?How can I connect to Android with ADB over TCP?No TIME_WAIT connections, why?Too many connections to memcached in TIME_WAIT stateHttpListener leaves connections in TIME_WAITPyQt5 and asyncio: yield from never finishesAsyncio decode utf-8 with StreamReaderWhy TIME_WAIT state is needed by active close side, but not the passive close side?TypeError: _request() got an unexpected keyword argument 'cookies' (aiohttp)
Hi I have a script that kept pooling port state of my device, this is the simplified version.
When connection is success ( device is exist ) then I close the connection the state of connection become TIME_WAIT. On time this connection is pilling and reach the maximum connection allowed by os ( If I'm remember )
Any idea which part should I fix, I use port 53 for example but on real app I check for multiple port like ssh, vnc, etc.
I run the script on ubuntu 18.04 with python 3.5.6
import asyncio
import ipaddress
import sys
async def check_port(ip, port, timeout=1):
conn = None
response = False
writer = None
try:
conn = asyncio.open_connection(ip, port)
reader, writer = await asyncio.wait_for(conn, timeout=timeout)
response = True
except asyncio.CancelledError:
print("asyncio cancel")
except:
response = False
finally:
if writer is not None:
writer.close()
if conn is not None:
conn.close()
print("Closing connection :".format(ip, port))
print(": ".format(ip, port, response))
async def poll_status():
ips = [str(ip) for ip in ipaddress.IPv4Network("192.168.1.0/24")]
while True:
try:
tasks = [check_port(ip, 53) for ip in ips]
await asyncio.wait(tasks)
except asyncio.CancelledError:
break
except KeyboardInterrupt:
break
except:
pass
await asyncio.sleep(1)
async def shutdown(task):
task.cancel()
await task
await asyncio.sleep(1)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(poll_status())
try:
loop.run_forever()
except:
pass
finally:
loop.run_until_complete(asyncio.wait([shutdown(task)]))
loop.close()
Connection kept pilling up like this ( output from "netstat -nput | grep TIME_WAIT" )
192.168.1.1 is my router, so it success in check port but leave a lot of unclosed connection. It took a long time for the connection to be remove
tcp 0 0 192.168.1.4:42102 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:42582 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:46560 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:39428 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:45806 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:44752 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:40726 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:49864 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:38812 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:48464 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:41372 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:43408 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:47360 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:45478 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:41904 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:40160 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:46196 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:48744 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:49554 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:47774 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:39370 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:43994 192.168.1.1:53 TIME_WAIT -
python-3.x tcp python-asyncio time-wait
add a comment |
Hi I have a script that kept pooling port state of my device, this is the simplified version.
When connection is success ( device is exist ) then I close the connection the state of connection become TIME_WAIT. On time this connection is pilling and reach the maximum connection allowed by os ( If I'm remember )
Any idea which part should I fix, I use port 53 for example but on real app I check for multiple port like ssh, vnc, etc.
I run the script on ubuntu 18.04 with python 3.5.6
import asyncio
import ipaddress
import sys
async def check_port(ip, port, timeout=1):
conn = None
response = False
writer = None
try:
conn = asyncio.open_connection(ip, port)
reader, writer = await asyncio.wait_for(conn, timeout=timeout)
response = True
except asyncio.CancelledError:
print("asyncio cancel")
except:
response = False
finally:
if writer is not None:
writer.close()
if conn is not None:
conn.close()
print("Closing connection :".format(ip, port))
print(": ".format(ip, port, response))
async def poll_status():
ips = [str(ip) for ip in ipaddress.IPv4Network("192.168.1.0/24")]
while True:
try:
tasks = [check_port(ip, 53) for ip in ips]
await asyncio.wait(tasks)
except asyncio.CancelledError:
break
except KeyboardInterrupt:
break
except:
pass
await asyncio.sleep(1)
async def shutdown(task):
task.cancel()
await task
await asyncio.sleep(1)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(poll_status())
try:
loop.run_forever()
except:
pass
finally:
loop.run_until_complete(asyncio.wait([shutdown(task)]))
loop.close()
Connection kept pilling up like this ( output from "netstat -nput | grep TIME_WAIT" )
192.168.1.1 is my router, so it success in check port but leave a lot of unclosed connection. It took a long time for the connection to be remove
tcp 0 0 192.168.1.4:42102 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:42582 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:46560 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:39428 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:45806 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:44752 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:40726 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:49864 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:38812 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:48464 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:41372 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:43408 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:47360 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:45478 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:41904 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:40160 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:46196 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:48744 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:49554 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:47774 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:39370 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:43994 192.168.1.1:53 TIME_WAIT -
python-3.x tcp python-asyncio time-wait
add a comment |
Hi I have a script that kept pooling port state of my device, this is the simplified version.
When connection is success ( device is exist ) then I close the connection the state of connection become TIME_WAIT. On time this connection is pilling and reach the maximum connection allowed by os ( If I'm remember )
Any idea which part should I fix, I use port 53 for example but on real app I check for multiple port like ssh, vnc, etc.
I run the script on ubuntu 18.04 with python 3.5.6
import asyncio
import ipaddress
import sys
async def check_port(ip, port, timeout=1):
conn = None
response = False
writer = None
try:
conn = asyncio.open_connection(ip, port)
reader, writer = await asyncio.wait_for(conn, timeout=timeout)
response = True
except asyncio.CancelledError:
print("asyncio cancel")
except:
response = False
finally:
if writer is not None:
writer.close()
if conn is not None:
conn.close()
print("Closing connection :".format(ip, port))
print(": ".format(ip, port, response))
async def poll_status():
ips = [str(ip) for ip in ipaddress.IPv4Network("192.168.1.0/24")]
while True:
try:
tasks = [check_port(ip, 53) for ip in ips]
await asyncio.wait(tasks)
except asyncio.CancelledError:
break
except KeyboardInterrupt:
break
except:
pass
await asyncio.sleep(1)
async def shutdown(task):
task.cancel()
await task
await asyncio.sleep(1)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(poll_status())
try:
loop.run_forever()
except:
pass
finally:
loop.run_until_complete(asyncio.wait([shutdown(task)]))
loop.close()
Connection kept pilling up like this ( output from "netstat -nput | grep TIME_WAIT" )
192.168.1.1 is my router, so it success in check port but leave a lot of unclosed connection. It took a long time for the connection to be remove
tcp 0 0 192.168.1.4:42102 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:42582 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:46560 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:39428 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:45806 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:44752 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:40726 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:49864 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:38812 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:48464 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:41372 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:43408 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:47360 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:45478 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:41904 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:40160 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:46196 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:48744 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:49554 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:47774 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:39370 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:43994 192.168.1.1:53 TIME_WAIT -
python-3.x tcp python-asyncio time-wait
Hi I have a script that kept pooling port state of my device, this is the simplified version.
When connection is success ( device is exist ) then I close the connection the state of connection become TIME_WAIT. On time this connection is pilling and reach the maximum connection allowed by os ( If I'm remember )
Any idea which part should I fix, I use port 53 for example but on real app I check for multiple port like ssh, vnc, etc.
I run the script on ubuntu 18.04 with python 3.5.6
import asyncio
import ipaddress
import sys
async def check_port(ip, port, timeout=1):
conn = None
response = False
writer = None
try:
conn = asyncio.open_connection(ip, port)
reader, writer = await asyncio.wait_for(conn, timeout=timeout)
response = True
except asyncio.CancelledError:
print("asyncio cancel")
except:
response = False
finally:
if writer is not None:
writer.close()
if conn is not None:
conn.close()
print("Closing connection :".format(ip, port))
print(": ".format(ip, port, response))
async def poll_status():
ips = [str(ip) for ip in ipaddress.IPv4Network("192.168.1.0/24")]
while True:
try:
tasks = [check_port(ip, 53) for ip in ips]
await asyncio.wait(tasks)
except asyncio.CancelledError:
break
except KeyboardInterrupt:
break
except:
pass
await asyncio.sleep(1)
async def shutdown(task):
task.cancel()
await task
await asyncio.sleep(1)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(poll_status())
try:
loop.run_forever()
except:
pass
finally:
loop.run_until_complete(asyncio.wait([shutdown(task)]))
loop.close()
Connection kept pilling up like this ( output from "netstat -nput | grep TIME_WAIT" )
192.168.1.1 is my router, so it success in check port but leave a lot of unclosed connection. It took a long time for the connection to be remove
tcp 0 0 192.168.1.4:42102 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:42582 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:46560 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:39428 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:45806 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:44752 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:40726 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:49864 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:38812 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:48464 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:41372 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:43408 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:47360 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:45478 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:41904 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:40160 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:46196 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:48744 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:49554 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:47774 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:39370 192.168.1.1:53 TIME_WAIT -
tcp 0 0 192.168.1.4:43994 192.168.1.1:53 TIME_WAIT -
python-3.x tcp python-asyncio time-wait
python-3.x tcp python-asyncio time-wait
edited Mar 8 at 16:52
Aleph Aleph
3,1651720
3,1651720
asked Mar 8 at 12:50
Wiryono LauwWiryono Lauw
101112
101112
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I'm not expert in network stuff, not sure if this answer will help, but here's my two cents.
First thing about netstat output. It's related router and seems to be unrelated to you OS limits. Fast googling shows following:
TIME_WAIT indicates that local endpoint (this side) has closed the
connection. The connection is being kept around so that any delayed
packets can be matched to the connection and handled appropriately.
The connections will be removed when they time out within four
minutes.
Seems to be your code closed connection, i.e. doing everything right.
However I have no idea how router will handle on increasing number of such connections.
Let's now consider your code.
What you doing at line asyncio.wait(tasks)
is running all your checks parallely. Depending on number of ips
it can be too much. High chances you can benefit from using asyncio.Semaphore to limit maximum number of parallel checks. It'll look following way:
sem = asyncio.Semaphore(100)
async def check_port(ip, port, timeout=1):
async with sem:
# main code here
You can also read this answer to see real example of using semaphore.
Next things you may need to fix is how you handle CancelledError
:
except asyncio.CancelledError:
print("asyncio cancel")
This is not how task should react on this exception. It never should suppress it, only outer code can do it. Read this answer to see how to do it.
except:
response = False
You never do such thing. Please read this topic for details.
You should instead at least do something like following:
except Exception as exc: # catch Exception or it's subclasses only
logging.exception(exc) # log for purpose not to miss exception you can fix
response = False
Regarding sempahore, I use that in my actual code. It's like a pool it help managing all await request either a coroutine function or not ( run with executor ). It is very good. two thumbs up :D
– Wiryono Lauw
Mar 8 at 23:17
For asyncio.CancelledError I mainly use for debug, sometime it's missing , so I put on every coroutine function to test :D, my bad. I have logging everywhere in the real app for the exception, just remove it in this example for simpler purpose
– Wiryono Lauw
Mar 8 at 23:20
I read about TIME_WAIT, it actually will reach limit when reach 65k ( max source port is used up ). On new open connection it will use different source port then the on in TIME_WAIT state which will pilling up in time. If using SO_REUSEADDR I think it doesn't fit my scenario, because I have to check the same device ( same destination ip and port multiple time )
– Wiryono Lauw
Mar 8 at 23:23
In the actual app I check multiple port and other stuff for single device, each check will open a connection. I'm using agent-less approach. so reaching that 65K is possible. The solution I think of is to delay the next iteration to minute ( The TIME_WAIT is remove around 1 minute ), but this will beat the purpose ...
– Wiryono Lauw
Mar 8 at 23:28
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55063606%2fasyncio-close-connection-leave-connection-in-time-wait-state%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'm not expert in network stuff, not sure if this answer will help, but here's my two cents.
First thing about netstat output. It's related router and seems to be unrelated to you OS limits. Fast googling shows following:
TIME_WAIT indicates that local endpoint (this side) has closed the
connection. The connection is being kept around so that any delayed
packets can be matched to the connection and handled appropriately.
The connections will be removed when they time out within four
minutes.
Seems to be your code closed connection, i.e. doing everything right.
However I have no idea how router will handle on increasing number of such connections.
Let's now consider your code.
What you doing at line asyncio.wait(tasks)
is running all your checks parallely. Depending on number of ips
it can be too much. High chances you can benefit from using asyncio.Semaphore to limit maximum number of parallel checks. It'll look following way:
sem = asyncio.Semaphore(100)
async def check_port(ip, port, timeout=1):
async with sem:
# main code here
You can also read this answer to see real example of using semaphore.
Next things you may need to fix is how you handle CancelledError
:
except asyncio.CancelledError:
print("asyncio cancel")
This is not how task should react on this exception. It never should suppress it, only outer code can do it. Read this answer to see how to do it.
except:
response = False
You never do such thing. Please read this topic for details.
You should instead at least do something like following:
except Exception as exc: # catch Exception or it's subclasses only
logging.exception(exc) # log for purpose not to miss exception you can fix
response = False
Regarding sempahore, I use that in my actual code. It's like a pool it help managing all await request either a coroutine function or not ( run with executor ). It is very good. two thumbs up :D
– Wiryono Lauw
Mar 8 at 23:17
For asyncio.CancelledError I mainly use for debug, sometime it's missing , so I put on every coroutine function to test :D, my bad. I have logging everywhere in the real app for the exception, just remove it in this example for simpler purpose
– Wiryono Lauw
Mar 8 at 23:20
I read about TIME_WAIT, it actually will reach limit when reach 65k ( max source port is used up ). On new open connection it will use different source port then the on in TIME_WAIT state which will pilling up in time. If using SO_REUSEADDR I think it doesn't fit my scenario, because I have to check the same device ( same destination ip and port multiple time )
– Wiryono Lauw
Mar 8 at 23:23
In the actual app I check multiple port and other stuff for single device, each check will open a connection. I'm using agent-less approach. so reaching that 65K is possible. The solution I think of is to delay the next iteration to minute ( The TIME_WAIT is remove around 1 minute ), but this will beat the purpose ...
– Wiryono Lauw
Mar 8 at 23:28
add a comment |
I'm not expert in network stuff, not sure if this answer will help, but here's my two cents.
First thing about netstat output. It's related router and seems to be unrelated to you OS limits. Fast googling shows following:
TIME_WAIT indicates that local endpoint (this side) has closed the
connection. The connection is being kept around so that any delayed
packets can be matched to the connection and handled appropriately.
The connections will be removed when they time out within four
minutes.
Seems to be your code closed connection, i.e. doing everything right.
However I have no idea how router will handle on increasing number of such connections.
Let's now consider your code.
What you doing at line asyncio.wait(tasks)
is running all your checks parallely. Depending on number of ips
it can be too much. High chances you can benefit from using asyncio.Semaphore to limit maximum number of parallel checks. It'll look following way:
sem = asyncio.Semaphore(100)
async def check_port(ip, port, timeout=1):
async with sem:
# main code here
You can also read this answer to see real example of using semaphore.
Next things you may need to fix is how you handle CancelledError
:
except asyncio.CancelledError:
print("asyncio cancel")
This is not how task should react on this exception. It never should suppress it, only outer code can do it. Read this answer to see how to do it.
except:
response = False
You never do such thing. Please read this topic for details.
You should instead at least do something like following:
except Exception as exc: # catch Exception or it's subclasses only
logging.exception(exc) # log for purpose not to miss exception you can fix
response = False
Regarding sempahore, I use that in my actual code. It's like a pool it help managing all await request either a coroutine function or not ( run with executor ). It is very good. two thumbs up :D
– Wiryono Lauw
Mar 8 at 23:17
For asyncio.CancelledError I mainly use for debug, sometime it's missing , so I put on every coroutine function to test :D, my bad. I have logging everywhere in the real app for the exception, just remove it in this example for simpler purpose
– Wiryono Lauw
Mar 8 at 23:20
I read about TIME_WAIT, it actually will reach limit when reach 65k ( max source port is used up ). On new open connection it will use different source port then the on in TIME_WAIT state which will pilling up in time. If using SO_REUSEADDR I think it doesn't fit my scenario, because I have to check the same device ( same destination ip and port multiple time )
– Wiryono Lauw
Mar 8 at 23:23
In the actual app I check multiple port and other stuff for single device, each check will open a connection. I'm using agent-less approach. so reaching that 65K is possible. The solution I think of is to delay the next iteration to minute ( The TIME_WAIT is remove around 1 minute ), but this will beat the purpose ...
– Wiryono Lauw
Mar 8 at 23:28
add a comment |
I'm not expert in network stuff, not sure if this answer will help, but here's my two cents.
First thing about netstat output. It's related router and seems to be unrelated to you OS limits. Fast googling shows following:
TIME_WAIT indicates that local endpoint (this side) has closed the
connection. The connection is being kept around so that any delayed
packets can be matched to the connection and handled appropriately.
The connections will be removed when they time out within four
minutes.
Seems to be your code closed connection, i.e. doing everything right.
However I have no idea how router will handle on increasing number of such connections.
Let's now consider your code.
What you doing at line asyncio.wait(tasks)
is running all your checks parallely. Depending on number of ips
it can be too much. High chances you can benefit from using asyncio.Semaphore to limit maximum number of parallel checks. It'll look following way:
sem = asyncio.Semaphore(100)
async def check_port(ip, port, timeout=1):
async with sem:
# main code here
You can also read this answer to see real example of using semaphore.
Next things you may need to fix is how you handle CancelledError
:
except asyncio.CancelledError:
print("asyncio cancel")
This is not how task should react on this exception. It never should suppress it, only outer code can do it. Read this answer to see how to do it.
except:
response = False
You never do such thing. Please read this topic for details.
You should instead at least do something like following:
except Exception as exc: # catch Exception or it's subclasses only
logging.exception(exc) # log for purpose not to miss exception you can fix
response = False
I'm not expert in network stuff, not sure if this answer will help, but here's my two cents.
First thing about netstat output. It's related router and seems to be unrelated to you OS limits. Fast googling shows following:
TIME_WAIT indicates that local endpoint (this side) has closed the
connection. The connection is being kept around so that any delayed
packets can be matched to the connection and handled appropriately.
The connections will be removed when they time out within four
minutes.
Seems to be your code closed connection, i.e. doing everything right.
However I have no idea how router will handle on increasing number of such connections.
Let's now consider your code.
What you doing at line asyncio.wait(tasks)
is running all your checks parallely. Depending on number of ips
it can be too much. High chances you can benefit from using asyncio.Semaphore to limit maximum number of parallel checks. It'll look following way:
sem = asyncio.Semaphore(100)
async def check_port(ip, port, timeout=1):
async with sem:
# main code here
You can also read this answer to see real example of using semaphore.
Next things you may need to fix is how you handle CancelledError
:
except asyncio.CancelledError:
print("asyncio cancel")
This is not how task should react on this exception. It never should suppress it, only outer code can do it. Read this answer to see how to do it.
except:
response = False
You never do such thing. Please read this topic for details.
You should instead at least do something like following:
except Exception as exc: # catch Exception or it's subclasses only
logging.exception(exc) # log for purpose not to miss exception you can fix
response = False
answered Mar 8 at 17:32
Mikhail GerasimovMikhail Gerasimov
14.8k44072
14.8k44072
Regarding sempahore, I use that in my actual code. It's like a pool it help managing all await request either a coroutine function or not ( run with executor ). It is very good. two thumbs up :D
– Wiryono Lauw
Mar 8 at 23:17
For asyncio.CancelledError I mainly use for debug, sometime it's missing , so I put on every coroutine function to test :D, my bad. I have logging everywhere in the real app for the exception, just remove it in this example for simpler purpose
– Wiryono Lauw
Mar 8 at 23:20
I read about TIME_WAIT, it actually will reach limit when reach 65k ( max source port is used up ). On new open connection it will use different source port then the on in TIME_WAIT state which will pilling up in time. If using SO_REUSEADDR I think it doesn't fit my scenario, because I have to check the same device ( same destination ip and port multiple time )
– Wiryono Lauw
Mar 8 at 23:23
In the actual app I check multiple port and other stuff for single device, each check will open a connection. I'm using agent-less approach. so reaching that 65K is possible. The solution I think of is to delay the next iteration to minute ( The TIME_WAIT is remove around 1 minute ), but this will beat the purpose ...
– Wiryono Lauw
Mar 8 at 23:28
add a comment |
Regarding sempahore, I use that in my actual code. It's like a pool it help managing all await request either a coroutine function or not ( run with executor ). It is very good. two thumbs up :D
– Wiryono Lauw
Mar 8 at 23:17
For asyncio.CancelledError I mainly use for debug, sometime it's missing , so I put on every coroutine function to test :D, my bad. I have logging everywhere in the real app for the exception, just remove it in this example for simpler purpose
– Wiryono Lauw
Mar 8 at 23:20
I read about TIME_WAIT, it actually will reach limit when reach 65k ( max source port is used up ). On new open connection it will use different source port then the on in TIME_WAIT state which will pilling up in time. If using SO_REUSEADDR I think it doesn't fit my scenario, because I have to check the same device ( same destination ip and port multiple time )
– Wiryono Lauw
Mar 8 at 23:23
In the actual app I check multiple port and other stuff for single device, each check will open a connection. I'm using agent-less approach. so reaching that 65K is possible. The solution I think of is to delay the next iteration to minute ( The TIME_WAIT is remove around 1 minute ), but this will beat the purpose ...
– Wiryono Lauw
Mar 8 at 23:28
Regarding sempahore, I use that in my actual code. It's like a pool it help managing all await request either a coroutine function or not ( run with executor ). It is very good. two thumbs up :D
– Wiryono Lauw
Mar 8 at 23:17
Regarding sempahore, I use that in my actual code. It's like a pool it help managing all await request either a coroutine function or not ( run with executor ). It is very good. two thumbs up :D
– Wiryono Lauw
Mar 8 at 23:17
For asyncio.CancelledError I mainly use for debug, sometime it's missing , so I put on every coroutine function to test :D, my bad. I have logging everywhere in the real app for the exception, just remove it in this example for simpler purpose
– Wiryono Lauw
Mar 8 at 23:20
For asyncio.CancelledError I mainly use for debug, sometime it's missing , so I put on every coroutine function to test :D, my bad. I have logging everywhere in the real app for the exception, just remove it in this example for simpler purpose
– Wiryono Lauw
Mar 8 at 23:20
I read about TIME_WAIT, it actually will reach limit when reach 65k ( max source port is used up ). On new open connection it will use different source port then the on in TIME_WAIT state which will pilling up in time. If using SO_REUSEADDR I think it doesn't fit my scenario, because I have to check the same device ( same destination ip and port multiple time )
– Wiryono Lauw
Mar 8 at 23:23
I read about TIME_WAIT, it actually will reach limit when reach 65k ( max source port is used up ). On new open connection it will use different source port then the on in TIME_WAIT state which will pilling up in time. If using SO_REUSEADDR I think it doesn't fit my scenario, because I have to check the same device ( same destination ip and port multiple time )
– Wiryono Lauw
Mar 8 at 23:23
In the actual app I check multiple port and other stuff for single device, each check will open a connection. I'm using agent-less approach. so reaching that 65K is possible. The solution I think of is to delay the next iteration to minute ( The TIME_WAIT is remove around 1 minute ), but this will beat the purpose ...
– Wiryono Lauw
Mar 8 at 23:28
In the actual app I check multiple port and other stuff for single device, each check will open a connection. I'm using agent-less approach. so reaching that 65K is possible. The solution I think of is to delay the next iteration to minute ( The TIME_WAIT is remove around 1 minute ), but this will beat the purpose ...
– Wiryono Lauw
Mar 8 at 23:28
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55063606%2fasyncio-close-connection-leave-connection-in-time-wait-state%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown