Spaces:
Sleeping
Sleeping
saifisvibinn commited on
Commit ·
cb9aca4
1
Parent(s): 2226eb2
Add better error handling for background processing thread startup
Browse files
app.py
CHANGED
|
@@ -478,27 +478,60 @@ def upload_files():
|
|
| 478 |
# Process files in background threads
|
| 479 |
# Read file data before starting threads to avoid "read of closed file" error
|
| 480 |
threads = []
|
|
|
|
| 481 |
for file in files:
|
| 482 |
if file and file.filename.endswith('.pdf'):
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 496 |
|
| 497 |
# Return task ID immediately
|
| 498 |
return jsonify({
|
| 499 |
'task_id': task_id,
|
| 500 |
'message': 'Processing started',
|
| 501 |
-
'total_files':
|
| 502 |
})
|
| 503 |
|
| 504 |
|
|
|
|
| 478 |
# Process files in background threads
|
| 479 |
# Read file data before starting threads to avoid "read of closed file" error
|
| 480 |
threads = []
|
| 481 |
+
started_count = 0
|
| 482 |
for file in files:
|
| 483 |
if file and file.filename.endswith('.pdf'):
|
| 484 |
+
try:
|
| 485 |
+
# Read file data into memory before starting background thread
|
| 486 |
+
# This prevents "read of closed file" errors when request context ends
|
| 487 |
+
file_data = file.read()
|
| 488 |
+
file.seek(0) # Reset file pointer for potential reuse
|
| 489 |
+
filename = file.filename
|
| 490 |
+
|
| 491 |
+
if not file_data:
|
| 492 |
+
logger.warning(f"Empty file data for {filename}")
|
| 493 |
+
with _progress_lock:
|
| 494 |
+
_progress_tracker[task_id]['results'].append({
|
| 495 |
+
'filename': filename,
|
| 496 |
+
'error': 'File is empty or could not be read'
|
| 497 |
+
})
|
| 498 |
+
continue
|
| 499 |
+
|
| 500 |
+
thread = threading.Thread(
|
| 501 |
+
target=process_file_background,
|
| 502 |
+
args=(task_id, file_data, filename, extraction_mode)
|
| 503 |
+
)
|
| 504 |
+
thread.daemon = True
|
| 505 |
+
thread.start()
|
| 506 |
+
threads.append(thread)
|
| 507 |
+
started_count += 1
|
| 508 |
+
logger.info(f"Started background thread for {filename}")
|
| 509 |
+
except Exception as e:
|
| 510 |
+
logger.error(f"Failed to start processing thread for {file.filename}: {e}")
|
| 511 |
+
import traceback
|
| 512 |
+
logger.error(traceback.format_exc())
|
| 513 |
+
with _progress_lock:
|
| 514 |
+
_progress_tracker[task_id]['results'].append({
|
| 515 |
+
'filename': file.filename,
|
| 516 |
+
'error': f'Failed to start processing: {str(e)}'
|
| 517 |
+
})
|
| 518 |
+
|
| 519 |
+
# Check if any threads were started
|
| 520 |
+
if started_count == 0:
|
| 521 |
+
with _progress_lock:
|
| 522 |
+
_progress_tracker[task_id]['status'] = 'error'
|
| 523 |
+
_progress_tracker[task_id]['message'] = 'No files could be processed. Check file format and size.'
|
| 524 |
+
return jsonify({
|
| 525 |
+
'error': 'No files could be processed',
|
| 526 |
+
'task_id': task_id,
|
| 527 |
+
'message': 'Processing could not be started. Please check your files and try again.'
|
| 528 |
+
}), 400
|
| 529 |
|
| 530 |
# Return task ID immediately
|
| 531 |
return jsonify({
|
| 532 |
'task_id': task_id,
|
| 533 |
'message': 'Processing started',
|
| 534 |
+
'total_files': started_count
|
| 535 |
})
|
| 536 |
|
| 537 |
|