Python 3.4: "ReferenceError: weakly-referenced object no longer exists" in __del__ of a class which refers to a Session object

I got the following error when running my code in Python 3.4 (Python 2.7 and 3.3 are fine):

```
Exception ignored in: <bound method API.__del__ of <u115.api.API object at 0x109dadb00>>
Traceback (most recent call last):
  File "/Users/shichao/envs/115wangpan-dl3/lib/python3.4/site-packages/u115/api.py", line 148, in __del__
    if self.auto_logout and self.has_logged_in:
  File "/Users/shichao/envs/115wangpan-dl3/lib/python3.4/site-packages/u115/api.py", line 195, in has_logged_in
    r = self.http.get(self.passport.checkpoint_url, params=params)
  File "/Users/shichao/envs/115wangpan-dl3/lib/python3.4/site-packages/u115/api.py", line 36, in get
    r = self.session.get(url, params=params)
  File "/Users/shichao/envs/115wangpan-dl3/lib/python3.4/site-packages/requests/sessions.py", line 469, in get
    return self.request('GET', url, **kwargs)
  File "/Users/shichao/envs/115wangpan-dl3/lib/python3.4/site-packages/requests/sessions.py", line 457, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/shichao/envs/115wangpan-dl3/lib/python3.4/site-packages/requests/sessions.py", line 563, in send
    adapter = self.get_adapter(url=request.url)
  File "/Users/shichao/envs/115wangpan-dl3/lib/python3.4/site-packages/requests/sessions.py", line 636, in get_adapter
    for (prefix, adapter) in self.adapters.items():
  File "/Users/shichao/envs/115wangpan-dl3/lib/python3.4/_collections_abc.py", line 484, in __iter__
    for key in self._mapping:
  File "/Users/shichao/envs/115wangpan-dl3/lib/python3.4/collections/__init__.py", line 87, in __iter__
    curr = root.next
ReferenceError: weakly-referenced object no longer exists
```

The source code is here: https://github.com/shichao-an/115wangpan/blob/master/u115/api.py#L147

I think maybe it's related to the features change in 3.4 pertinent to `__del__` and weakref: 
- https://docs.python.org/3/library/gc.html#gc.garbage
- https://docs.python.org/3/library/weakref.html#comparing-finalizers-with-del-methods

I wonder if there is any workaround.


I see this error too!


So this is coming out of the standard library. The standard library implementation of an OrderedDict (which is included in Python 3.4)  uses a [weakref for the root reference](https://hg.python.org/cpython/file/534b26837a13/Lib/collections/__init__.py#l74). This is a bug in Python 3.4 and should be reported on bugs.python.org.


As it seems that nobody bothered to report this I did it at http://bugs.python.org/issue23841


Anyone try Python 3.5 to see if it goes away?


@vvolkman it seems as though this is "Not a bug" to the upstream CPython develoeprs. Their recommendation is for people to [stop using `__del__`](https://bugs.python.org/msg240246) and instead use `weakref.finalize`. I'm going to check to ensure this doesn't actually affect requests.


What are the real consequences of this bug?  Is data lost?  If it is just a case of a spurious warning, I'll ignore it and get back to coding my apps.  Sorry for the dumb question but the implications to app that uses requests are unclear.


It shouldn't affect you unless you try to use `__del__` and do something with a session on the object where you've defined that. The "real consequences" are an unhandled exception that appears to come from requests but is a result of your application using `__del__`. This will affect all versions of Python after and including 3.4 (as best I can tell).


It seems that upstream (https://bugs.python.org/issue23841) has given suggestions to how requests can fix this and has closed it as "not a bug" in python. I'd appreciate it if this issue could be re-opened and someone could implement their suggested fix.

@SwartzCr The suggestion is "don't do anything complex in `__del__`, and the source of the `__del__` method is user code. I have no problem saying that users shouldn't make HTTP requests from `__del__`.

oh I see - I think I got confused about where the offending code was