title: ESP8266 Micropython: [Errno 103] ECONNABORTED\r\n') after some time
body: <p>I am having issues running the following script for a longer period of time:</p>
  
  <p>I use ampy to execute the script on the ESP:</p>
  
  <pre><code>sudo ampy --port /dev/ttyUSB0 run photoresistor.py
  </code></pre>
  
  <p>photoresistor.py:</p>
  
  <pre><code>#!/usr/bin/env python3
  
  import machine
  import network
  from time import sleep
  from urllib.urequest import urlopen
  import json
  
  wifiap = network.WLAN(network.AP_IF)
  wifiap.active(False)
  routercon = network.WLAN(network.STA_IF)
  routercon.active(True)
  routercon.ifconfig(('10.0.0.128','255.255.255.0','10.0.0.138','10.0.0.138'))
  routercon.connect('mywifi', '123')
  while not routercon.isconnected():
      pass
  
  posturl=('http://10.0.0.156:23102/rest/v2/send')
  
  adc = machine.ADC(0)
  gc.enable()
  
  while True:
      value = adc.read()
      if value &lt; 200:
         message = {'username': 'test', 'message': value, 'chatid': 'test', 'password': 'test', 'notifyself': 'false'}
         r = urlopen(posturl, data=json.dumps(message).encode())
         r.close()
      gc.collect()
      sleep(1)
  </code></pre>
  
  <p>It works as expected in the beginning but after some time I get the following stacktrace:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/usr/local/bin/ampy", line 11, in &lt;module&gt;
      sys.exit(cli())
    File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 764, in __call__
      return self.main(*args, **kwargs)
    File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 717, in main
      rv = self.invoke(ctx)
    File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 1137, in invoke
      return _process_result(sub_ctx.command.invoke(sub_ctx))
    File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 956, in invoke
      return ctx.invoke(self.callback, **ctx.params)
    File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 555, in invoke
      return callback(*args, **kwargs)
    File "/usr/local/lib/python3.6/dist-packages/ampy/cli.py", line 337, in run
      output = board_files.run(local_file, not no_output)
    File "/usr/local/lib/python3.6/dist-packages/ampy/files.py", line 303, in run
      out = self._pyboard.execfile(filename)
    File "/usr/local/lib/python3.6/dist-packages/ampy/pyboard.py", line 273, in execfile
      return self.exec_(pyfile)
    File "/usr/local/lib/python3.6/dist-packages/ampy/pyboard.py", line 267, in exec_
      raise PyboardError('exception', ret, ret_err)
  ampy.pyboard.PyboardError: ('exception', b'', b'Traceback (most recent call last):\r\n  File "&lt;stdin&gt;", line 28, in &lt;module&gt;\r\n  File "urequests.py", line 152, in post\r\n  File "urequests.py", line 89, in request\r\nOSError: [Errno 103] ECONNABORTED\r\n')
  </code></pre>
  
  <p>No idea what to do.
  I tried to play around with the garbage collection but it didn't help.</p>
  
  <p>I suspect that the board doesn't clean up sockets properly.
  If the board is sending post requests quickly in the loop (every second for 1 minute) and let it sit afterwards for a short period of time it fails quickly with above ECONNABORTED.</p>
  
  <p>If the board sends post requests more slowly (like 2 in a minute) it takes way longer for it to fail. To conclude: I suspect the OS does not properly clean up resources and still has active connections after <code>r.close()</code> or I am overseeing something in the code.</p>
  
  <p>I am not sure what else I can do to make sure these sockets are closed.</p>
  
  <p>EDIT:
  I found out it fails on connect (<a href="https://github.com/micropython/micropython-lib/blob/master/urllib.urequest/urllib/urequest.py" rel="nofollow noreferrer">https://github.com/micropython/micropython-lib/blob/master/urllib.urequest/urllib/urequest.py</a>):</p>
  
  <p>line 28:</p>
  
  <pre><code>s.connect(ai[-1])
  </code></pre>
  
  <p>however <code>routercon.isconnected()</code> returns true:</p>
  
  <pre><code>&gt;&gt;&gt; routercon.isconnected()
  True
  &gt;&gt;&gt; 
  </code></pre>
  
  <p>How can it be that altough there is a active connection I am unable to send an http post request?</p>
  
  <p>EDIT2:</p>
  
  <p>When this happens sometimes I also can't post to another endpoint e.g. the test server with the same webservice</p>
  
  <pre><code>&gt;&gt;&gt; r = urlopen(posturl, data=json.dumps(message).encode())
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "urllib/urequest.py", line 28, in urlopen
  OSError: [Errno 103] ECONNABORTED
  &gt;&gt;&gt; r = urlopen("http://10.0.0.8:23102/rest/v2/send", data=json.dumps(message).encode())
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "urllib/urequest.py", line 28, in urlopen
  OSError: [Errno 103] ECONNABORTED
  &gt;&gt;&gt; 
  </code></pre>
  
  <p>Interestingly a http get to google works:</p>
  
  <pre><code>&gt;&gt;&gt; r = urlopen("http://www.google.com")
  &gt;&gt;&gt;
  </code></pre>
  
  <p>If I let it sit idle for some time http post start to work again.
  Could it be that the OS is performing a cleanup in the background?</p>