fix the issue

This commit is contained in:
saqib mir
2026-06-02 11:16:49 +05:30
parent 2068737fc9
commit 581ef030f2
11 changed files with 705 additions and 70 deletions
-50
View File
@@ -1,50 +0,0 @@
import sys
import os
import threading
import time
sys.path.insert(0, os.path.abspath('gateway'))
import pdfengine
def worker(thread_id: int, iterations: int):
# Try different operations concurrently
pdf_files = ['utf-8.pdf']
for i in range(iterations):
for pdf_file in pdf_files:
try:
filepath = os.path.abspath(f"corpus/fonts/{pdf_file}")
doc = pdfengine.PdfDocument.load_from_file(filepath)
page = doc.get_page(0)
# Repeated get_fonts() (tests document font cache mutex)
fonts = doc.get_fonts(0, -1)
# Repeated extract_text_with_bounds() (tests GlyphCache and text extraction mutex)
glyphs = page.extract_text_with_bounds()
except Exception as e:
print(f"[Thread {thread_id}] Error: {e}")
def run_concurrency_test(num_threads: int, iterations: int):
print(f"Starting concurrency test with {num_threads} threads, {iterations} iterations each...")
start_time = time.time()
threads = []
for i in range(num_threads):
t = threading.Thread(target=worker, args=(i, iterations))
threads.append(t)
t.start()
for t in threads:
t.join()
elapsed = time.time() - start_time
print(f"Concurrency test completed in {elapsed:.2f} seconds.")
print("No crashes or deadlocks detected.")
if __name__ == "__main__":
# Run 10 threads
run_concurrency_test(10, 20)
# Run 50 threads
run_concurrency_test(50, 10)
-57
View File
@@ -1,57 +0,0 @@
import sys
import os
import psutil
import gc
import time
sys.path.insert(0, os.path.abspath('gateway'))
import pdfengine
def get_memory_mb():
process = psutil.Process(os.getpid())
return process.memory_info().rss / (1024 * 1024)
def validate_memory(iterations=1000):
pdf_file = os.path.abspath("corpus/fonts/utf-8.pdf")
print("Memory Validation Test")
print("-" * 30)
gc.collect()
start_mem = get_memory_mb()
print(f"Initial Memory: {start_mem:.2f} MB")
for i in range(iterations):
# Repeated PDF open/close, cache operations
doc = pdfengine.PdfDocument.load_from_file(pdf_file)
fonts = doc.get_fonts(0, -1)
page = doc.get_page(0)
text = page.extract_text()
glyphs = page.extract_text_with_bounds()
# Explicitly delete references
del glyphs
del text
del page
del fonts
del doc
if (i + 1) % 200 == 0:
gc.collect()
curr_mem = get_memory_mb()
print(f"Iteration {i + 1}: {curr_mem:.2f} MB (Delta: {curr_mem - start_mem:.2f} MB)")
gc.collect()
end_mem = get_memory_mb()
print(f"Final Memory: {end_mem:.2f} MB")
delta = end_mem - start_mem
print(f"Total Delta: {delta:.2f} MB")
if delta > 5.0:
print("WARNING: Possible memory leak detected!")
else:
print("SUCCESS: Memory usage is stable. No leaks detected.")
if __name__ == "__main__":
validate_memory()