fix: Thread-Management für Kopiervorgänge verbessert und Fehlerbehandlung hinzugefügt

This commit is contained in:
2026-05-09 10:32:04 +02:00
parent 299bf98f13
commit 50295c1b51

9
app.py
View File

@@ -76,6 +76,7 @@ copy_state = {
'phase': 'idle', 'phase': 'idle',
} }
copy_lock = threading.Lock() copy_lock = threading.Lock()
_copy_thread: threading.Thread | None = None
def load_state(): def load_state():
global copy_state global copy_state
@@ -922,16 +923,20 @@ def r_status():
@app.route('/api/copy/start', methods=['POST']) @app.route('/api/copy/start', methods=['POST'])
def r_start(): def r_start():
global _copy_thread
with copy_lock: with copy_lock:
if copy_state['running']: if copy_state['running']:
return jsonify(error='Bereits aktiv'), 400 return jsonify(error='Bereits aktiv'), 400
if _copy_thread is not None and _copy_thread.is_alive():
return jsonify(error='Abbruch läuft noch, bitte einen Moment warten'), 400
cfg = load_cfg() cfg = load_cfg()
devs = usb_devices() devs = usb_devices()
src = next((d for d in devs if d['usb_port'] == cfg.get('source_port')), None) src = next((d for d in devs if d['usb_port'] == cfg.get('source_port')), None)
dst = next((d for d in devs if d['usb_port'] == cfg.get('dest_port')), None) dst = next((d for d in devs if d['usb_port'] == cfg.get('dest_port')), None)
if not src: return jsonify(error='Quellgerät nicht gefunden'), 400 if not src: return jsonify(error='Quellgerät nicht gefunden'), 400
if not dst: return jsonify(error='Zielgerät nicht gefunden'), 400 if not dst: return jsonify(error='Zielgerät nicht gefunden'), 400
threading.Thread(target=do_copy, args=(src, dst, cfg), daemon=True).start() _copy_thread = threading.Thread(target=do_copy, args=(src, dst, cfg), daemon=True)
_copy_thread.start()
return jsonify(ok=True) return jsonify(ok=True)
@app.route('/api/copy/cancel', methods=['POST']) @app.route('/api/copy/cancel', methods=['POST'])
@@ -2306,7 +2311,7 @@ async function poll(){
} }
sum.textContent=''; bS.style.display='none'; bC.style.display=''; time.textContent=''; sum.textContent=''; bS.style.display='none'; bC.style.display=''; time.textContent='';
}else{ }else{
bS.style.display=''; bC.style.display=''; cf.textContent=''; bS.style.display=''; bC.style.display='none'; cf.textContent='';
eta.style.display='none'; spd.style.display='none'; pfiles.style.display='none'; pbytes.style.display='none'; pp.style.display='none'; eta.style.display='none'; spd.style.display='none'; pfiles.style.display='none'; pbytes.style.display='none'; pp.style.display='none';
if(c.error){ if(c.error){
tx.className='st-headline st-err'; tx.textContent='Fehler: '+c.error; tx.className='st-headline st-err'; tx.textContent='Fehler: '+c.error;