fixed nasty bug where tor circuits was causing gevent to leak threads

This commit is contained in:
Kevin Froman 2020-03-20 03:50:48 -05:00
parent 022fbaa1af
commit e6181bdd1f
12 changed files with 47 additions and 39 deletions

View file

@ -1,8 +1,14 @@
"""
Onionr - Private P2P Communication
"""Onionr - Private P2P Communication.
This module serializes various data pieces for use in other modules, in particular the web api
Serialize various node information
"""
import json
from gevent import sleep
from psutil import Process
from coredb import blockmetadb
import communicator
"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@ -18,11 +24,6 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import json # noqa
import time # noqa
from coredb import blockmetadb # noqa
import communicator # noqa
class SerializedData:
def __init__(self):
@ -34,19 +35,21 @@ class SerializedData:
etc
}
"""
def get_stats(self):
"""Return statistics about our node"""
stats = {}
proc = Process()
try:
self._too_many
except AttributeError:
time.sleep(1)
sleep(1)
comm_inst = self._too_many.get(communicator.OnionrCommunicatorDaemon, args=(self._too_many,))
connected = []
[connected.append(x) for x in comm_inst.onlinePeers if x not in connected]
[connected.append(x) for x in comm_inst.onlinePeers if x not in connected]
stats['uptime'] = comm_inst.getUptime()
stats['connectedNodes'] = '\n'.join(connected)
stats['blockCount'] = len(blockmetadb.get_block_list())
stats['blockQueueCount'] = len(comm_inst.blockQueue)
stats['threads'] = proc.num_threads()
return json.dumps(stats)

View file

@ -3,7 +3,6 @@
"""
import json
from gevent import sleep
from stem import CircStatus
@ -29,6 +28,7 @@ class TorStats:
def __init__(self):
self.circuits = {}
self.json_data = ""
self.controller = None
def get_json(self):
"""Refresh circuits then serialize them into form:
@ -36,6 +36,8 @@ class TorStats:
"nodes": list of tuples containing fingerprint and nickname strings"
"purpose": https://stem.torproject.org/api/control.html#stem.CircPurpose
"""
if self.controller is None:
self.controller = get_controller()
self.get_circuits()
json_serialized = {}
for circuit in self.circuits.keys():
@ -52,7 +54,7 @@ class TorStats:
def get_circuits(self):
"""Update the circuit dictionary"""
circuits = {}
for circ in list(sorted(get_controller().get_circuits())):
for circ in list(sorted(self.controller.get_circuits())):
if circ.status != CircStatus.BUILT:
continue
circuits[circ.id] = (circ.path, circ.purpose)