netbox-community / pynetbox

Python API client library for Netbox.
Apache License 2.0
567 stars 168 forks source link

pyNetBox throws exception ConnectionError when allocating an IP, even if it was successfully allocated #526

Closed Yuliya-H closed 1 year ago

Yuliya-H commented 1 year ago

Trying to allocate IP in a few threads in parallel via :

avail_ips = DetailEndpoint(iprange, "available-ips", custom_return=IpAddresses)
address = avail_ips.create()

Getting ConnectionError which raises BadStatusLine error, even when IP is allocated:

http.client.BadStatusLine: {"id":384,"url":"http://192.168.122.209:8000/api/ipam/ip-addresses/384/","display":">[10.10.0.19/24](https://10.10.0.19/24)","family":{"value":4,"label":"IPv4"},"address":"[10.10.0.19/24](https://10.10.0.19/24)","vrf":{"id":1,"url":"http://192.168.122.209:8000/api/ipam/vrfs/1/","display":"admin","name":"admin","rd":null},"tenant":null,"status":{"value":"active","label":"Active"},"role":null,"assigned_object_type":null,"assigned_object_id":null,"assigned_object":null,"nat_inside":null,"nat_outside":[],"dns_name":"","description":"","tags":[],"custom_fields":{},"created":"2022-12-21T13:08:20.549255Z","last_updated":"2022-12-21T13:08:20.549284Z"}HTTP/1.1 201 Created

One more problem here is that the HTTP status goes after the response json.

markkuleinio commented 1 year ago

pynetbox does not raise ConnectionErrors, it's some underlying module that's doing that.

Are you using the same pynetbox.api object (= the same requests.Session) in all those threads? I'm not a Python threading expert but I'm thinking if that could cause the responses to get messed up. See also: https://github.com/psf/requests/issues/2766

Yuliya-H commented 1 year ago

Yes , indeed I'm using one Session. I will try to separate and test this way, thanks

Yuliya-H commented 1 year ago

Everything worked OK when using separate Sessions.