Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions FlagEmbedding/abc/inference/AbsReranker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from abc import ABC, abstractmethod
from sys import is_finalizing
from typing import Any, Union, List, Tuple, Dict, Literal, Optional

import multiprocessing as mp
Expand Down Expand Up @@ -228,8 +229,11 @@ def compute_score(
**kwargs)
return scores

def __del__(self):
self.stop_self_pool()
def __del__(self, _is_finalizing=is_finalizing):
# multiprocessing terminates and joins our daemon workers at exit,
# before finalizers can run with partially cleared module globals.
if not _is_finalizing():
self.stop_self_pool()

@abstractmethod
def compute_score_single_gpu(
Expand Down
33 changes: 33 additions & 0 deletions tests/test_reranker_pool_shutdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import subprocess
import sys
from textwrap import dedent


def test_reranker_cleanup_during_collection_and_shutdown():
result = subprocess.run(
[sys.executable, "-c", dedent("""
import gc
import os
from FlagEmbedding.abc.inference.AbsReranker import AbsReranker

class Probe(AbsReranker):
def compute_score_single_gpu(self, *args, **kwargs):
pass

def stop_self_pool(self, write=os.write):
write(1, self.marker)

def probe(marker):
instance = object.__new__(Probe)
instance.marker = marker
instance.cycle = instance
return instance

probe(b'collected\\n')
gc.collect()
retained_until_shutdown = probe(b'shutdown\\n')
""")],
capture_output=True, text=True, check=True,
)
assert result.stdout.splitlines() == ["collected"]
assert "Exception ignored" not in result.stderr