fix: prevent WebAssembly reinitialization on memory errors
**Motivations :** - Les erreurs de mémoire causent des tentatives multiples d'initialisation WebAssembly - Services.initializing reste défini après une erreur, causant de nouvelles tentatives - Les pages réessayent indéfiniment même en cas d'erreur mémoire fatale **Modifications :** - Réinitialiser Services.initializing à null dans le catch pour éviter les tentatives multiples - Détecter les erreurs de mémoire et arrêter immédiatement les tentatives - Ajouter la détection d'erreurs de mémoire dans wallet-setup, birthday-setup et block-sync - Afficher un message clair à l'utilisateur pour actualiser la page en cas d'erreur mémoire - Améliorer les messages d'erreur pour indiquer que l'actualisation de la page est nécessaire **Pages affectées :** - src/services/service.ts (réinitialisation de initializing et détection mémoire) - src/pages/wallet-setup/wallet-setup.ts (détection erreurs mémoire) - src/pages/birthday-setup/birthday-setup.ts (détection erreurs mémoire) - src/pages/block-sync/block-sync.ts (détection erreurs mémoire)
This commit is contained in:
parent
b3af85d3a0
commit
9de7f1a5ed
@ -28,9 +28,20 @@ document.addEventListener('DOMContentLoaded', async () => {
|
||||
}
|
||||
console.log('🔄 Getting existing services instance...');
|
||||
|
||||
// Utiliser l'instance existante des services
|
||||
const services = await Services.getInstance();
|
||||
console.log('✅ Services instance obtained successfully');
|
||||
// Utiliser l'instance existante des services avec gestion des erreurs de mémoire
|
||||
let services;
|
||||
try {
|
||||
services = await Services.getInstance();
|
||||
console.log('✅ Services instance obtained successfully');
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
if (errorMessage.includes('Out of memory') || errorMessage.includes('insufficient memory')) {
|
||||
console.error('🚫 Memory error detected');
|
||||
updateStatus('❌ Erreur: Mémoire insuffisante. Veuillez actualiser la page.', 'error');
|
||||
throw new Error('WebAssembly initialization failed due to insufficient memory. Please refresh the page.');
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Vérifier les prérequis en base de données
|
||||
updateStatus('🔍 Vérification des prérequis...', 'loading');
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
document.addEventListener("DOMContentLoaded", async () => {
|
||||
console.log("🔄 Block sync page loaded");
|
||||
|
||||
|
||||
const status = document.getElementById("status") as HTMLElement;
|
||||
const progressBar = document.getElementById("progressBar") as HTMLElement;
|
||||
const continueBtn = document.getElementById("continueBtn") as HTMLButtonElement;
|
||||
@ -95,7 +95,16 @@ document.addEventListener("DOMContentLoaded", async () => {
|
||||
console.log('✅ Services initialized successfully');
|
||||
break;
|
||||
} catch (error) {
|
||||
console.log(`⏳ Services not ready yet (attempt ${attempts + 1}/${maxAttempts}):`, (error as Error).message);
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
console.log(`⏳ Services not ready yet (attempt ${attempts + 1}/${maxAttempts}):`, errorMessage);
|
||||
|
||||
// Si c'est une erreur de mémoire, arrêter immédiatement
|
||||
if (errorMessage.includes('Out of memory') || errorMessage.includes('insufficient memory')) {
|
||||
console.error('🚫 Memory error detected - stopping retry attempts');
|
||||
updateStatus('❌ Erreur: Mémoire insuffisante. Veuillez actualiser la page.', 'error');
|
||||
throw new Error('WebAssembly initialization failed due to insufficient memory. Please refresh the page.');
|
||||
}
|
||||
|
||||
attempts++;
|
||||
if (attempts >= maxAttempts) {
|
||||
throw new Error(`Services failed to initialize after ${maxAttempts} attempts.`);
|
||||
@ -158,7 +167,7 @@ document.addEventListener("DOMContentLoaded", async () => {
|
||||
if (currentBlockHeight === -1 || currentBlockHeight === 0) {
|
||||
console.log('⚠️ Block height not available, connecting to relays...');
|
||||
await services.connectAllRelays();
|
||||
|
||||
|
||||
// Attendre que le handshake arrive et que chain_tip soit défini
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const timeout = setTimeout(() => {
|
||||
@ -212,30 +221,30 @@ document.addEventListener("DOMContentLoaded", async () => {
|
||||
updateProgress(60);
|
||||
|
||||
console.log(`🔄 Starting real block scan from ${lastScan} to ${currentBlockHeight}...`);
|
||||
|
||||
|
||||
// Utiliser updateDeviceBlockHeight qui gère la synchronisation si nécessaire
|
||||
// Cette méthode vérifie si last_scan < currentBlockHeight et synchronise si nécessaire
|
||||
try {
|
||||
await services.updateDeviceBlockHeight();
|
||||
console.log('✅ Block scan completed successfully');
|
||||
|
||||
|
||||
// Vérifier que la mise à jour a été sauvegardée
|
||||
const finalWallet = await services.getDeviceFromDatabase();
|
||||
if (finalWallet?.sp_wallet?.last_scan) {
|
||||
const finalLastScan = finalWallet.sp_wallet.last_scan;
|
||||
console.log('✅ Wallet updated with last_scan:', finalLastScan);
|
||||
|
||||
|
||||
// Finalisation
|
||||
updateStatus('✅ Synchronisation terminée avec succès!', 'success');
|
||||
updateProgress(100);
|
||||
updateSyncItem('blocksScanned', finalLastScan.toString(), 'completed');
|
||||
updateSyncItem('blocksToScan', blocksToScan.toString(), 'completed');
|
||||
|
||||
|
||||
// Activer le bouton continuer
|
||||
if (continueBtn) {
|
||||
continueBtn.disabled = false;
|
||||
}
|
||||
|
||||
|
||||
console.log('🎉 Block sync completed successfully');
|
||||
} else {
|
||||
throw new Error('Failed to verify wallet update - last_scan not found');
|
||||
@ -248,9 +257,9 @@ document.addEventListener("DOMContentLoaded", async () => {
|
||||
} catch (error) {
|
||||
console.error('❌ Error during block sync:', error);
|
||||
const errorMessage = (error as Error).message;
|
||||
|
||||
|
||||
updateStatus(`❌ Erreur: ${errorMessage}`, 'error');
|
||||
|
||||
|
||||
// Si l'erreur est liée aux prérequis, rediriger vers la page appropriée
|
||||
if (errorMessage.includes('PBKDF2') || errorMessage.includes('security')) {
|
||||
console.log('⚠️ Security error detected, redirecting to security-setup...');
|
||||
|
||||
@ -78,24 +78,31 @@ document.addEventListener('DOMContentLoaded', async () => {
|
||||
services = await Services.getInstance();
|
||||
console.log('✅ Services initialized successfully');
|
||||
break;
|
||||
} catch (error) {
|
||||
console.log(`⏳ Services not ready yet (attempt ${attempts + 1}/${maxAttempts}):`, error instanceof Error ? error.message : String(error));
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
console.log(`⏳ Services not ready yet (attempt ${attempts + 1}/${maxAttempts}):`, errorMessage);
|
||||
|
||||
// Diagnostic plus détaillé
|
||||
if (attempts === 5) {
|
||||
console.log('🔍 Diagnostic: Checking memory usage...');
|
||||
if ((performance as any).memory) {
|
||||
const memory = (performance as any).memory;
|
||||
console.log(`📊 Memory usage: ${Math.round(memory.usedJSHeapSize / 1024 / 1024)}MB / ${Math.round(memory.totalJSHeapSize / 1024 / 1024)}MB`);
|
||||
}
|
||||
}
|
||||
|
||||
attempts++;
|
||||
if (attempts >= maxAttempts) {
|
||||
throw new Error(`Services failed to initialize after ${maxAttempts} attempts. This may be due to high memory usage or WebAssembly initialization issues.`);
|
||||
}
|
||||
await new Promise(resolve => setTimeout(resolve, delayMs));
|
||||
// Si c'est une erreur de mémoire, arrêter immédiatement
|
||||
if (errorMessage.includes('Out of memory') || errorMessage.includes('insufficient memory')) {
|
||||
console.error('🚫 Memory error detected - stopping retry attempts');
|
||||
updateStatus('❌ Erreur: Mémoire insuffisante. Veuillez actualiser la page.', 'error');
|
||||
throw new Error('WebAssembly initialization failed due to insufficient memory. Please refresh the page.');
|
||||
}
|
||||
|
||||
// Diagnostic plus détaillé
|
||||
if (attempts === 5) {
|
||||
console.log('🔍 Diagnostic: Checking memory usage...');
|
||||
if ((performance as any).memory) {
|
||||
const memory = (performance as any).memory;
|
||||
console.log(`📊 Memory usage: ${Math.round(memory.usedJSHeapSize / 1024 / 1024)}MB / ${Math.round(memory.totalJSHeapSize / 1024 / 1024)}MB`);
|
||||
}
|
||||
}
|
||||
|
||||
attempts++;
|
||||
if (attempts >= maxAttempts) {
|
||||
throw new Error(`Services failed to initialize after ${maxAttempts} attempts. This may be due to high memory usage or WebAssembly initialization issues.`);
|
||||
}
|
||||
await new Promise(resolve => setTimeout(resolve, delayMs));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('❌ Services not available:', error);
|
||||
|
||||
@ -331,6 +331,16 @@ export default class Services {
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Service initialization failed:', error);
|
||||
// Réinitialiser initializing pour permettre une nouvelle tentative après un délai
|
||||
Services.initializing = null;
|
||||
|
||||
// Si c'est une erreur de mémoire, ne pas réessayer immédiatement
|
||||
const errorMessage = (error as Error).message || String(error);
|
||||
if (errorMessage.includes('Out of memory') || errorMessage.includes('memory')) {
|
||||
console.error('🚫 Memory error detected - cannot retry immediately');
|
||||
throw new Error('WebAssembly initialization failed due to insufficient memory. Please refresh the page.');
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user