Skip to content

Commit

Permalink
Remove use of multiprocessing values from fresh_name
Browse files Browse the repository at this point in the history
Cross-process unique names have been broken since #119.
  • Loading branch information
Calvin-L committed Aug 9, 2020
1 parent eb8cc18 commit d5e659c
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions cozy/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
import sys
import os
import inspect
from multiprocessing import Value
import threading
import ctypes
import tempfile
import shutil

Expand Down Expand Up @@ -294,24 +292,30 @@ def __lt__(self, other):
def __repr__(self):
return "FrozenDict({!r})".format(list(self.items()))

_name_counter = Value(ctypes.c_uint64, 0)
_name_counter = 0

def fresh_name(hint : str = "name", omit : {str} = ()) -> str:
"""Generate a new name.
The returned name is guaranteed to be distinct from all names previously
returned by `fresh_name` (even across threads and forked processes), and
is is also guaranteed to be distinct from all names in `omit`.
returned by `fresh_name`, and is is also guaranteed to be distinct from all
names in `omit`.
The `hint` parameter will be used in the generated name.
CAUTION:
- This procedure is not thread-safe.
- Names generated by this procedure are only unique within this process.
(This is relevant because names generated in multiprocessing jobs might
overlap with each other and with names generated by the parent process.)
"""
global _name_counter
name = None
with _name_counter.get_lock():
i = _name_counter.value
while name is None or name in omit:
name = "_{}{}".format(hint, i)
i += 1
_name_counter.value = i
i = _name_counter
while name is None or name in omit:
name = "_{}{}".format(hint, i)
i += 1
_name_counter = i
return name

def capitalize(s):
Expand Down

0 comments on commit d5e659c

Please sign in to comment.