def assert_no_greenlet_growth(self):
        from gevent._greenlet_primitives import get_reachable_greenlets
        clean = self.__clean
        clean()
        count_before = len(get_reachable_greenlets())

        yield

        count_after = len(get_reachable_greenlets())
        if count_after > count_before:
            # We could be off by exactly 1. Not entirely clear where.
            # But it only happens the first time.
            count_after -= 1
        # If we were run in multiple process, our count could actually have
        # gone down due to the GC's we did.
        self.assertEqual(count_after, count_before)
Beispiel #2
0
    def _forest(cls):
        from gevent._greenlet_primitives import get_reachable_greenlets
        main_greenlet = cls._root_greenlet(getcurrent())

        trees = {}
        roots = {}
        current_tree = roots[main_greenlet] = trees[main_greenlet] = cls(
            main_greenlet)

        glets = get_reachable_greenlets()

        for ob in glets:
            spawn_parent = cls.__spawning_parent(ob)

            if spawn_parent is None:
                root = cls._root_greenlet(ob)
                try:
                    tree = roots[root]
                except KeyError:  # pragma: no cover
                    tree = GreenletTree(root)
                    roots[root] = trees[root] = tree
            else:
                try:
                    tree = trees[spawn_parent]
                except KeyError:  # pragma: no cover
                    tree = trees[spawn_parent] = cls(spawn_parent)

            try:
                child_tree = trees[ob]
            except KeyError:
                trees[ob] = child_tree = cls(ob)
            tree.add_child(child_tree)

        return roots, current_tree
Beispiel #3
0
    def _forest(cls):
        from gevent._greenlet_primitives import get_reachable_greenlets
        main_greenlet = cls._root_greenlet(getcurrent())

        trees = {}
        roots = {}
        current_tree = roots[main_greenlet] = trees[main_greenlet] = cls(main_greenlet)

        glets = get_reachable_greenlets()

        for ob in glets:
            spawn_parent = cls.__spawning_parent(ob)

            if spawn_parent is None:
                root = cls._root_greenlet(ob)
                try:
                    tree = roots[root]
                except KeyError: # pragma: no cover
                    tree = GreenletTree(root)
                    roots[root] = trees[root] = tree
            else:
                try:
                    tree = trees[spawn_parent]
                except KeyError: # pragma: no cover
                    tree = trees[spawn_parent] = cls(spawn_parent)

            try:
                child_tree = trees[ob]
            except KeyError:
                trees[ob] = child_tree = cls(ob)
            tree.add_child(child_tree)

        return roots, current_tree
    def test_join_in_new_thread_doesnt_leak_hub_or_greenlet(self):
        # https://github.com/gevent/gevent/issues/1601
        import threading
        import gc
        from gevent._greenlet_primitives import get_reachable_greenlets

        def _clean():
            for _ in range(2):
                while gc.collect():
                    pass

        _clean()
        count_before = len(get_reachable_greenlets())

        def thread_main():
            g = gevent.Greenlet(run=lambda: 0)
            g.start()
            g.join()
            hub = gevent.get_hub()
            hub.join()
            hub.destroy(destroy_loop=True)
            del hub

        def tester(main):
            t = threading.Thread(target=main)
            t.start()
            t.join()

            _clean()

        for _ in range(10):
            tester(thread_main)

        del tester
        del thread_main

        count_after = len(get_reachable_greenlets())
        if count_after > count_before:
            # We could be off by exactly 1. Not entirely clear where.
            # But it only happens the first time.
            count_after -= 1
        # If we were run in multiple process, our count could actually have
        # gone down due to the GC's we did.
        self.assertEqual(count_after, count_before)
Beispiel #5
0
    def _forest(cls):
        from gevent._greenlet_primitives import get_reachable_greenlets
        main_greenlet = cls._root_greenlet(getcurrent())

        trees = {}  # greenlet -> GreenletTree
        roots = {}  # root greenlet -> GreenletTree
        current_tree = roots[main_greenlet] = trees[main_greenlet] = cls(
            main_greenlet)
        current_tree.is_current_tree = True

        root_greenlet = cls._root_greenlet
        glets = get_reachable_greenlets()

        for ob in glets:
            spawn_parent = cls.__spawning_parent(ob)

            if spawn_parent is None:
                # spawn parent is dead, or raw greenlet.
                # reparent under the root.
                spawn_parent = root_greenlet(ob)

            if spawn_parent is root_greenlet(
                    spawn_parent) and spawn_parent not in roots:
                assert spawn_parent not in trees
                trees[spawn_parent] = roots[spawn_parent] = cls(spawn_parent)

            try:
                parent_tree = trees[spawn_parent]
            except KeyError:  # pragma: no cover
                parent_tree = trees[spawn_parent] = cls(spawn_parent)

            try:
                # If the child also happened to be a spawning parent,
                # we could have seen it before; the reachable greenlets
                # are in no particular order.
                child_tree = trees[ob]
            except KeyError:
                trees[ob] = child_tree = cls(ob)
            parent_tree.add_child(child_tree)

        return roots, current_tree