Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
P403n1x87 committed Aug 11, 2022
1 parent 6581ada commit e98a0ca
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
17 changes: 16 additions & 1 deletion ddtrace/internal/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,13 @@ def __delitem__(self, name):

def __getattribute__(self, name):
# type: (str) -> Any
if PY2 and name == "keys":
# This is a potential attempt to make a copy of sys.modules using
# dict(sys.modules), so we take this change to update self to look
# like sys.modules.
super(ModuleWatchdog, self).clear()
super(ModuleWatchdog, self).update(self._modules)

try:
return super(ModuleWatchdog, self).__getattribute__("_modules").__getattribute__(name)
except AttributeError:
Expand Down Expand Up @@ -308,7 +315,15 @@ def find_module(self, fullname, path=None):
if not isinstance(loader, _ImportHookChainedLoader):
loader = _ImportHookChainedLoader(loader)

loader.add_callback(type(self), self.after_import)
if PY2:
# With Python 2 we don't get all the finder invoked, so we
# make sure we register all the callbacks at the earliest
# opportunity.
for finder in sys.meta_path:
if isinstance(finder, ModuleWatchdog):
loader.add_callback(type(finder), finder.after_import)
else:
loader.add_callback(type(self), self.after_import)

return loader

Expand Down
1 change: 1 addition & 0 deletions riotfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ def select_pys(min_version=MIN_PYTHON_VERSION, max_version=MAX_PYTHON_VERSION):
"structlog": latest,
# httpretty v1.0 drops python 2.7 support
"httpretty": "==0.9.7",
"gevent": latest,
},
)
],
Expand Down
57 changes: 57 additions & 0 deletions tests/tracer/test_forksafe.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys

import pytest
import six
Expand Down Expand Up @@ -285,3 +286,59 @@ def fn():
pid, status = os.waitpid(child, 0)
exit_code = os.WEXITSTATUS(status)
assert exit_code == 42


@pytest.mark.subprocess(
out="C\nT\nC\nT\nC\nT\n" if sys.platform == "darwin" else "C\nC\nC\nT\nT\nT\n",
err=None,
)
def test_gevent_reinit_patch():
import os
import sys

from ddtrace.internal import forksafe
from ddtrace.internal.periodic import PeriodicService

class TestService(PeriodicService):
def __init__(self):
super(TestService, self).__init__(interval=1.0)

def periodic(self):
print("T")

service = TestService()
service.start()

def restart_service():
global service

service.stop()
service = TestService()
service.start()

forksafe.register(restart_service)

import gevent # noqa

def run_child():
global service

# We mimic what gunicorn does in child processes
gevent.monkey.patch_all()
gevent.hub.reinit()

print("C")

gevent.sleep(1.1)

service.stop()

def fork_workers(num):
for _ in range(num):
if os.fork() == 0:
run_child()
sys.exit(0)

fork_workers(3)

service.stop()

0 comments on commit e98a0ca

Please sign in to comment.