Skip to content

Commit

Permalink
fix(tracer): fix file queue on read only file sytems (#10004)
Browse files Browse the repository at this point in the history
- ensure file queue will be properly disabled on read only file systems
- ensure the tmp file will not grow beyond the max file size of 8192
bytes (previously one more write was possible)
- small improvements

Multiple service name reports will be disabled on file systems where
python can create a temporary directory or file.

## Checklist
- [x] PR author has checked that all the criteria below are met
- The PR description includes an overview of the change
- The PR description articulates the motivation for the change
- The change includes tests OR the PR description describes a testing
strategy
- The PR description notes risks associated with the change, if any
- Newly-added code is easy to change
- The change follows the [library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
- The change includes or references documentation updates if necessary
- Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))

## Reviewer Checklist
- [ ] Reviewer has checked that all the criteria below are met 
- Title is accurate
- All changes are related to the pull request's stated goal
- Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- Testing strategy adequately addresses listed risks
- Newly-added code is easy to change
- Release note makes sense to a user of the library
- If necessary, author has acknowledged and discussed the performance
implications of this PR as reported in the benchmarks PR comment
- Backport labels are set in a manner that is consistent with the
[release branch maintenance
policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
  • Loading branch information
christophe-papazian authored Jul 31, 2024
1 parent 1710fe3 commit 2f7063b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
27 changes: 22 additions & 5 deletions ddtrace/internal/_file_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import typing

from ddtrace.internal._unpatched import unpatched_open
from ddtrace.internal.logger import get_logger


log = get_logger(__name__)


MAX_FILE_SIZE = 8192
Expand Down Expand Up @@ -56,23 +60,34 @@ class File_Queue:
"""A simple file-based queue implementation for multiprocess communication."""

def __init__(self) -> None:
self.directory = tempfile.gettempdir()
self.filename = os.path.join(self.directory, secrets.token_hex(8))
try:
self._directory: typing.Optional[str] = tempfile.gettempdir()
self.filename: typing.Optional[str] = os.path.join(self._directory, secrets.token_hex(8))
except Exception as e:
info = f"Failed to create a temporary file for the file queue. {e}"
log.debug(info)
self._directory = None
self.filename = None

def put(self, data: str) -> None:
"""Push a string to the queue."""
if self.filename is None:
return
try:
with open_file(self.filename, "ab") as f:
lock(f)
f.seek(0, os.SEEK_END)
if f.tell() < MAX_FILE_SIZE:
f.write((data + "\x00").encode())
dt = (data + "\x00").encode()
if f.tell() + len(dt) <= MAX_FILE_SIZE:
f.write(dt)
unlock(f)
except Exception: # nosec
pass

def get_all(self) -> typing.Set[str]:
"""Pop all unique strings from the queue."""
if self.filename is None:
return set()
try:
with open_file(self.filename, "r+b") as f:
lock(f)
Expand All @@ -82,7 +97,9 @@ def get_all(self) -> typing.Set[str]:
f.truncate()
unlock(f)
if data:
return set(data.split("\x00")[:-1])
res = data.split("\x00")
res.pop()
return set(res)
except Exception: # nosec
pass
return set()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
tracer: This fix resolves an issue where the tracer was not starting properly on a read-only file system.

0 comments on commit 2f7063b

Please sign in to comment.