Inherit helper_threading.StoppableThread from threading.Thread

and do random.seed() in its __init__
This commit is contained in:
Dmitri Bogomolov 2019-08-01 14:37:26 +03:00
parent a7a634be1b
commit ad2a2b3fb4
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13
17 changed files with 68 additions and 96 deletions

View File

@ -21,7 +21,6 @@ import json
import random # nosec
import socket
import subprocess
import threading
import time
from binascii import hexlify, unhexlify
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer
@ -32,7 +31,6 @@ from version import softwareVersion
import defaults
import helper_inbox
import helper_sent
import helper_threading
import network.stats
import proofofwork
import queues
@ -44,6 +42,7 @@ from bmconfigparser import BMConfigParser
from debug import logger
from helper_ackPayload import genAckPayload
from helper_sql import SqlBulkExecute, sqlExecute, sqlQuery, sqlStoredProcedure
from helper_threading import StoppableThread
from inventory import Inventory
str_chan = '[chan]'
@ -73,11 +72,10 @@ class StoppableXMLRPCServer(SimpleXMLRPCServer):
# This thread, of which there is only one, runs the API.
class singleAPI(threading.Thread, helper_threading.StoppableThread):
class singleAPI(StoppableThread):
"""API thread"""
def __init__(self):
threading.Thread.__init__(self, name="singleAPI")
self.initStop()
name = "singleAPI"
def stopThread(self):
super(singleAPI, self).stopThread()

View File

@ -1,6 +1,5 @@
import time
import threading
import hashlib
from binascii import hexlify
from pyelliptic import arithmetic
@ -19,12 +18,9 @@ from fallback import RIPEMD160Hash
from helper_threading import StoppableThread
class addressGenerator(threading.Thread, StoppableThread):
class addressGenerator(StoppableThread):
def __init__(self):
# QThread.__init__(self, parent)
threading.Thread.__init__(self, name="addressGenerator")
self.initStop()
name = "addressGenerator"
def stopThread(self):
try:

View File

@ -35,12 +35,13 @@ class objectProcessor(threading.Thread):
objects (msg, broadcast, pubkey, getpubkey) from the receiveDataThreads.
"""
def __init__(self):
threading.Thread.__init__(self, name="objectProcessor")
random.seed()
# It may be the case that the last time Bitmessage was running,
# the user closed it before it finished processing everything in the
# objectProcessorQueue. Assuming that Bitmessage wasn't closed
# forcefully, it should have saved the data in the queue into the
# objectprocessorqueue table. Let's pull it out.
threading.Thread.__init__(self, name="objectProcessor")
queryreturn = sqlQuery(
'''SELECT objecttype, data FROM objectprocessorqueue''')
for row in queryreturn:

View File

@ -21,7 +21,6 @@ resends msg messages in 5 days (then 10 days, then 20 days, etc...)
import gc
import os
import shared
import threading
import time
import tr
@ -36,14 +35,11 @@ import queues
import state
class singleCleaner(threading.Thread, StoppableThread):
class singleCleaner(StoppableThread):
name = "singleCleaner"
cycleLength = 300
expireDiscoveredPeers = 300
def __init__(self):
threading.Thread.__init__(self, name="singleCleaner")
self.initStop()
def run(self):
gc.disable()
timeWeLastClearedInventoryAndPubkeysTables = 0

View File

@ -7,7 +7,6 @@ src/class_singleWorker.py
from __future__ import division
import hashlib
import threading
import time
from binascii import hexlify, unhexlify
from struct import pack
@ -43,12 +42,11 @@ def sizeof_fmt(num, suffix='h/s'):
return "%.1f%s%s" % (num, 'Yi', suffix)
class singleWorker(threading.Thread, StoppableThread):
class singleWorker(StoppableThread):
"""Thread for performing PoW"""
def __init__(self):
threading.Thread.__init__(self, name="singleWorker")
self.initStop()
super(singleWorker, self).__init__(name="singleWorker")
proofofwork.init()
def stopThread(self):

View File

@ -6,7 +6,6 @@ src/class_smtpDeliver.py
import smtplib
import sys
import threading
import urlparse
from email.header import Header
from email.mime.text import MIMEText
@ -20,14 +19,11 @@ from helper_threading import StoppableThread
SMTPDOMAIN = "bmaddr.lan"
class smtpDeliver(threading.Thread, StoppableThread):
class smtpDeliver(StoppableThread):
"""SMTP client thread for delivery"""
name = "smtpDeliver"
_instance = None
def __init__(self):
threading.Thread.__init__(self, name="smtpDeliver")
self.initStop()
def stopThread(self):
try:
queues.UISignallerQueue.put(("stopThread", "data")) # pylint: disable=no-member

View File

@ -154,10 +154,10 @@ class smtpServerPyBitmessage(smtpd.SMTPServer):
continue
return
class smtpServer(threading.Thread, StoppableThread):
class smtpServer(StoppableThread):
def __init__(self, parent=None):
threading.Thread.__init__(self, name="smtpServerThread")
self.initStop()
super(smtpServer, self).__init__(name="smtpServerThread")
self.server = smtpServerPyBitmessage(('127.0.0.1', LISTENPORT), None)
def stopThread(self):

View File

@ -6,6 +6,11 @@ from pyelliptic.openssl import OpenSSL
NoneType = type(None)
def seed():
"""Initialize random number generator"""
random.seed()
def randomBytes(n):
"""Method randomBytes."""
try:

View File

@ -1,7 +1,9 @@
"""Helper threading perform all the threading operations."""
from contextlib import contextmanager
import threading
from contextlib import contextmanager
import helper_random
try:
import prctl
@ -22,7 +24,16 @@ else:
threading.Thread._Thread__bootstrap = _thread_name_hack
class StoppableThread(object):
class StoppableThread(threading.Thread):
name = None
def __init__(self, name=None):
if name:
self.name = name
super(StoppableThread, self).__init__(name=self.name)
self.initStop()
helper_random.seed()
def initStop(self):
self.stop = threading.Event()
self._stopped = False
@ -35,6 +46,7 @@ class StoppableThread(object):
class BusyError(threading.ThreadError):
pass
@contextmanager
def nonBlocking(lock):
locked = lock.acquire(False)

View File

@ -1,18 +1,13 @@
import Queue
import threading
import addresses
from helper_threading import StoppableThread
from network.connectionpool import BMConnectionPool
from queues import addrQueue
import protocol
import state
class AddrThread(threading.Thread, StoppableThread):
def __init__(self):
threading.Thread.__init__(self, name="AddrBroadcaster")
self.initStop()
self.name = "AddrBroadcaster"
class AddrThread(StoppableThread):
name = "AddrBroadcaster"
def run(self):
while not state.shutdown:
@ -28,7 +23,7 @@ class AddrThread(threading.Thread, StoppableThread):
except KeyError:
continue
#finish
# finish
addrQueue.iterate()
for i in range(len(chunk)):

View File

@ -1,4 +1,3 @@
import threading
import time
from bmconfigparser import BMConfigParser
@ -9,11 +8,10 @@ from network.connectionpool import BMConnectionPool
from network.udp import UDPSocket
import state
class AnnounceThread(threading.Thread, StoppableThread):
class AnnounceThread(StoppableThread):
def __init__(self):
threading.Thread.__init__(self, name="Announcer")
self.initStop()
self.name = "Announcer"
super(AnnounceThread, self).__init__(name="Announcer")
logger.info("init announce thread")
def run(self):

View File

@ -1,4 +1,3 @@
import threading
import time
import addresses
@ -12,7 +11,7 @@ from network.connectionpool import BMConnectionPool
from objectracker import missingObjects
class DownloadThread(threading.Thread, StoppableThread):
class DownloadThread(StoppableThread):
minPending = 200
maxRequestChunk = 1000
requestTimeout = 60
@ -20,9 +19,7 @@ class DownloadThread(threading.Thread, StoppableThread):
requestExpires = 3600
def __init__(self):
threading.Thread.__init__(self, name="Downloader")
self.initStop()
self.name = "Downloader"
super(DownloadThread, self).__init__(name="Downloader")
logger.info("init download thread")
self.lastCleaned = time.time()

View File

@ -1,16 +1,14 @@
import Queue
from random import randint, shuffle
import threading
import random
from time import time
import addresses
from bmconfigparser import BMConfigParser
import protocol
import state
from helper_threading import StoppableThread
from network.connectionpool import BMConnectionPool
from network.dandelion import Dandelion
from queues import invQueue
import protocol
import state
def handleExpiredDandelion(expired):
@ -33,11 +31,8 @@ def handleExpiredDandelion(expired):
i.objectsNewToThem[hashid] = time()
class InvThread(threading.Thread, StoppableThread):
def __init__(self):
threading.Thread.__init__(self, name="InvBroadcaster")
self.initStop()
self.name = "InvBroadcaster"
class InvThread(StoppableThread):
name = "InvBroadcaster"
def handleLocallyGenerated(self, stream, hashId):
Dandelion().addHash(hashId, stream=stream)
@ -80,7 +75,7 @@ class InvThread(threading.Thread, StoppableThread):
if connection == Dandelion().objectChildStem(inv[1]):
# Fluff trigger by RNG
# auto-ignore if config set to 0, i.e. dandelion is off
if randint(1, 100) >= state.dandelion:
if random.randint(1, 100) >= state.dandelion:
fluffs.append(inv[1])
# send a dinv only if the stem node supports dandelion
elif connection.services & protocol.NODE_DANDELION > 0:
@ -91,13 +86,15 @@ class InvThread(threading.Thread, StoppableThread):
fluffs.append(inv[1])
if fluffs:
shuffle(fluffs)
connection.append_write_buf(protocol.CreatePacket('inv', \
addresses.encodeVarint(len(fluffs)) + "".join(fluffs)))
random.shuffle(fluffs)
connection.append_write_buf(protocol.CreatePacket(
'inv', addresses.encodeVarint(len(fluffs)) +
"".join(fluffs)))
if stems:
shuffle(stems)
connection.append_write_buf(protocol.CreatePacket('dinv', \
addresses.encodeVarint(len(stems)) + "".join(stems)))
random.shuffle(stems)
connection.append_write_buf(protocol.CreatePacket(
'dinv', addresses.encodeVarint(len(stems)) +
"".join(stems)))
invQueue.iterate()
for i in range(len(chunk)):

View File

@ -1,4 +1,3 @@
import threading
import network.asyncore_pollchoose as asyncore
import state
@ -8,11 +7,9 @@ from network.connectionpool import BMConnectionPool
from queues import excQueue
class BMNetworkThread(threading.Thread, StoppableThread):
class BMNetworkThread(StoppableThread):
def __init__(self):
threading.Thread.__init__(self, name="Asyncore")
self.initStop()
self.name = "Asyncore"
super(BMNetworkThread, self).__init__(name="Asyncore")
logger.info("init asyncore thread")
def run(self):

View File

@ -1,27 +1,18 @@
import errno
import Queue
import socket
import sys
import threading
import time
import addresses
from bmconfigparser import BMConfigParser
from debug import logger
from helper_threading import StoppableThread
from inventory import Inventory
from network.connectionpool import BMConnectionPool
from network.bmproto import BMProto
from network.advanceddispatcher import UnknownStateError
from queues import receiveDataQueue
import protocol
import state
class ReceiveQueueThread(threading.Thread, StoppableThread):
class ReceiveQueueThread(StoppableThread):
def __init__(self, num=0):
threading.Thread.__init__(self, name="ReceiveQueue_%i" %(num))
self.initStop()
self.name = "ReceiveQueue_%i" % (num)
super(ReceiveQueueThread, self).__init__(name="ReceiveQueue_%i" % num)
logger.info("init receive queue thread %i", num)
def run(self):

View File

@ -2,7 +2,6 @@
src/network/uploadthread.py
"""
# pylint: disable=unsubscriptable-object
import threading
import time
import helper_random
@ -15,14 +14,12 @@ from network.dandelion import Dandelion
from randomtrackingdict import RandomTrackingDict
class UploadThread(threading.Thread, StoppableThread):
class UploadThread(StoppableThread):
"""This is a thread that uploads the objects that the peers requested from me """
maxBufSize = 2097152 # 2MB
def __init__(self):
threading.Thread.__init__(self, name="Uploader")
self.initStop()
self.name = "Uploader"
super(UploadThread, self).__init__(name="Uploader")
logger.info("init upload thread")
def run(self):

View File

@ -9,7 +9,6 @@ Reference: http://mattscodecave.com/posts/using-python-and-upnp-to-forward-a-por
import httplib
import socket
import threading
import time
import urllib2
from random import randint
@ -201,7 +200,7 @@ class Router: # pylint: disable=old-style-class
return resp
class uPnPThread(threading.Thread, StoppableThread):
class uPnPThread(StoppableThread):
"""Start a thread to handle UPnP activity"""
SSDP_ADDR = "239.255.255.250"
@ -211,7 +210,7 @@ class uPnPThread(threading.Thread, StoppableThread):
SSDP_ST = "urn:schemas-upnp-org:device:InternetGatewayDevice:1"
def __init__(self):
threading.Thread.__init__(self, name="uPnPThread")
super(uPnPThread, self).__init__(name="uPnPThread")
try:
self.extPort = BMConfigParser().getint('bitmessagesettings', 'extport')
except:
@ -223,7 +222,6 @@ class uPnPThread(threading.Thread, StoppableThread):
self.sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
self.sock.settimeout(5)
self.sendSleep = 60
self.initStop()
def run(self):
"""Start the thread to manage UPnP activity"""