pyroute2 icon indicating copy to clipboard operation
pyroute2 copied to clipboard

"address is not allocated"

Open tomkcook opened this issue 6 years ago • 4 comments

My code calls IPRoute.get_addr(family=socket.AF_INET, label='eth0') fairly frequently. After a bit, it starts doing this:

Traceback (most recent call last):
  File "InterfaceUtils.py", line 336, in valid_ip4_address
  File "pyroute2/iproute/linux.py", line 303, in get_addr
  File "pyroute2/iproute/linux.py", line 1271, in addr
  File "pyroute2/netlink/nlsocket.py", line 352, in nlm_request
  File "pyroute2/netlink/nlsocket.py", line 830, in nlm_request
  File "pyroute2/common.py", line 407, in alloc
  File "pyroute2/common.py", line 483, in free
KeyError: 'address is not allocated'

I'm not clear what's going on here - any suggestions?

tomkcook avatar Nov 12 '18 16:11 tomkcook

This is a threading issue. It can be readily reproduced with the following code:

import threading

def test():
    from pyroute2 import IPRoute
    import socket

    ipr = IPRoute()
    def test_thread():
        for j in range(10000):
            details = []
            for i in range(10000):
                ipr.get_addr(family=socket.AF_INET, label='wlp1s0')

    for i in range(10):
        threading.Thread(target=test_thread).start()

test()

Moving ipr = IPRoute() two lines down, so that there is one such object for each thread, eliminates the problem.

I will try to put together a pull request that addresses this tomorrow.

tomkcook avatar Nov 27 '18 17:11 tomkcook

Actually, I've decided to fix this in my code by making my instances of the IPRoute and IW objects thread-local, sorry.

tomkcook avatar Nov 28 '18 10:11 tomkcook

Just wanted to say that I encountered the same issue. Wish that the error message was more descriptive, as it really sent me on quite a wild goose chase. And unfortunately this issue page for some reason does not come up when googling " pyroute2 KeyError 'address is not allocated' ".

Indeed, it was solved by not having a single IPRoute object that's used across threads.

svet-b avatar Mar 05 '20 14:03 svet-b

IPRoute objects must be thread-safe, so if they fail to operate this way — it's a bug. I reopen the issue. @svet-b thanks for bringing it up.

svinota avatar Mar 05 '20 15:03 svinota