Merge pull request #840 from shocknet/crash-debug

throw on exit, not err
This commit is contained in:
Justin (shocknet) 2025-09-26 14:27:30 -04:00 committed by GitHub
commit 3d6a5f8b7d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 41 additions and 14 deletions

View file

@ -143,6 +143,7 @@ const sendToNostr: NostrSend = (initiator, data, relays) => {
} }
subProcessHandler.Send(initiator, data, relays) subProcessHandler.Send(initiator, data, relays)
} }
send({ type: 'ready' }) send({ type: 'ready' })
const supportedKinds = [21000, 21001, 21002, 21003] const supportedKinds = [21000, 21001, 21002, 21003]
export default class Handler { export default class Handler {
@ -174,8 +175,8 @@ export default class Handler {
if (!relay.connected) { if (!relay.connected) {
throw new Error("failed to connect to relay") throw new Error("failed to connect to relay")
} }
} catch (err) { } catch (err:any) {
log("failed to connect to relay, will try again in 2 seconds") log("failed to connect to relay, will try again in 2 seconds", err.message || err)
setTimeout(() => { setTimeout(() => {
this.Connect() this.Connect()
}, 2000) }, 2000)
@ -251,10 +252,16 @@ export default class Handler {
} }
async Send(initiator: SendInitiator, data: SendData, relays?: string[]) { async Send(initiator: SendInitiator, data: SendData, relays?: string[]) {
try {
const keys = this.GetSendKeys(initiator) const keys = this.GetSendKeys(initiator)
const privateKey = Buffer.from(keys.privateKey, 'hex') const privateKey = Buffer.from(keys.privateKey, 'hex')
const toSign = await this.handleSend(data, keys) const toSign = await this.handleSend(data, keys)
await Promise.all(toSign.map(ue => this.sendEvent(ue, keys, relays))) await Promise.all(toSign.map(ue => this.sendEvent(ue, keys, relays)))
} catch (e: any) {
this.log(ERROR, "failed to send event", e.message || e)
throw e
}
} }
async handleSend(data: SendData, keys: { name: string, privateKey: string, publicKey: string }): Promise<UnsignedEvent[]> { async handleSend(data: SendData, keys: { name: string, privateKey: string, publicKey: string }): Promise<UnsignedEvent[]> {

View file

@ -2,6 +2,7 @@ import { ChildProcess, fork } from 'child_process'
import { EnvCanBeInteger, EnvMustBeNonEmptyString } from "../helpers/envParser.js" import { EnvCanBeInteger, EnvMustBeNonEmptyString } from "../helpers/envParser.js"
import { NostrSettings, NostrEvent, ChildProcessRequest, ChildProcessResponse, SendData, SendInitiator } from "./handler.js" import { NostrSettings, NostrEvent, ChildProcessRequest, ChildProcessResponse, SendData, SendInitiator } from "./handler.js"
import { Utils } from '../helpers/utilsWrapper.js' import { Utils } from '../helpers/utilsWrapper.js'
import {getLogger, ERROR} from '../helpers/logger.js'
type EventCallback = (event: NostrEvent) => void type EventCallback = (event: NostrEvent) => void
@ -13,13 +14,22 @@ export default class NostrSubprocess {
childProcess: ChildProcess childProcess: ChildProcess
utils: Utils utils: Utils
awaitingPongs: (() => void)[] = [] awaitingPongs: (() => void)[] = []
log = getLogger({})
constructor(settings: NostrSettings, utils: Utils, eventCallback: EventCallback) { constructor(settings: NostrSettings, utils: Utils, eventCallback: EventCallback) {
this.utils = utils this.utils = utils
this.childProcess = fork("./build/src/services/nostr/handler") this.childProcess = fork("./build/src/services/nostr/handler")
this.childProcess.on("error", (error) => { this.childProcess.on("error", (error) => {
console.error("nostr subprocess error") this.log(ERROR, "nostr subprocess error", error)
throw error
}) })
this.childProcess.on("exit", (code) => {
this.log(ERROR, `nostr subprocess exited with code ${code}`)
if (!code) {
return
}
throw new Error(`nostr subprocess exited with code ${code}`)
})
this.childProcess.on("message", (message: ChildProcessResponse) => { this.childProcess.on("message", (message: ChildProcessResponse) => {
switch (message.type) { switch (message.type) {
case 'ready': case 'ready':

View file

@ -18,6 +18,7 @@ import { PickKeysByType } from 'typeorm/common/PickKeysByType.js';
import { deserializeResponseData, serializeRequest, WhereCondition } from './serializationHelpers.js'; import { deserializeResponseData, serializeRequest, WhereCondition } from './serializationHelpers.js';
import { Utils } from '../../helpers/utilsWrapper.js'; import { Utils } from '../../helpers/utilsWrapper.js';
import { ProcessMetrics } from '../tlv/processMetricsCollector.js'; import { ProcessMetrics } from '../tlv/processMetricsCollector.js';
import { getLogger, ERROR } from '../../helpers/logger.js';
export type TX<T> = (txId: string) => Promise<T> export type TX<T> = (txId: string) => Promise<T>
@ -28,7 +29,7 @@ export class StorageInterface extends EventEmitter {
private debug: boolean = false; private debug: boolean = false;
private utils: Utils private utils: Utils
private dbType: 'main' | 'metrics' private dbType: 'main' | 'metrics'
private log = getLogger({component: 'StorageInterface'})
constructor(utils: Utils) { constructor(utils: Utils) {
super(); super();
this.initializeSubprocess(); this.initializeSubprocess();
@ -56,14 +57,17 @@ export class StorageInterface extends EventEmitter {
}); });
this.process.on('error', (error: Error) => { this.process.on('error', (error: Error) => {
console.error('Storage processor error:', error); this.log(ERROR, 'Storage processor error:', error);
this.isConnected = false; this.isConnected = false;
throw error
}); });
this.process.on('exit', (code: number) => { this.process.on('exit', (code: number) => {
console.log(`Storage processor exited with code ${code}`); this.log(ERROR, `Storage processor exited with code ${code}`);
this.isConnected = false; this.isConnected = false;
if (!code) {
return
}
throw new Error(`Storage processor exited with code ${code}`)
}); });
this.isConnected = true; this.isConnected = true;

View file

@ -6,6 +6,7 @@ import { NostrSend, SendData, SendInitiator } from '../../nostr/handler';
import { WebRtcUserInfo } from '../../webRTC'; import { WebRtcUserInfo } from '../../webRTC';
import * as Types from '../../../../proto/autogenerated/ts/types.js' import * as Types from '../../../../proto/autogenerated/ts/types.js'
import { ProcessMetrics } from './processMetricsCollector'; import { ProcessMetrics } from './processMetricsCollector';
import { getLogger, ERROR } from '../../helpers/logger.js';
export type TlvStorageInterface = { export type TlvStorageInterface = {
AddTlv: (appId: string, dataName: string, tlv: Uint8Array) => Promise<number> AddTlv: (appId: string, dataName: string, tlv: Uint8Array) => Promise<number>
LoadLatest: (limit?: number) => Promise<LatestData> LoadLatest: (limit?: number) => Promise<LatestData>
@ -18,6 +19,7 @@ export class TlvStorageFactory extends EventEmitter {
private debug: boolean = false; private debug: boolean = false;
private _nostrSend: NostrSend = () => { throw new Error('nostr send not initialized yet') } private _nostrSend: NostrSend = () => { throw new Error('nostr send not initialized yet') }
private allowResetMetricsStorages: boolean private allowResetMetricsStorages: boolean
log = getLogger({component: 'TlvStorageFactory'})
constructor(allowResetMetricsStorages: boolean) { constructor(allowResetMetricsStorages: boolean) {
super(); super();
this.allowResetMetricsStorages = allowResetMetricsStorages this.allowResetMetricsStorages = allowResetMetricsStorages
@ -51,13 +53,17 @@ export class TlvStorageFactory extends EventEmitter {
}); });
this.process.on('error', (error: Error) => { this.process.on('error', (error: Error) => {
console.error('Tlv Storage processor error:', error); this.log(ERROR, 'Tlv Storage processor error:', error);
this.isConnected = false; this.isConnected = false;
}); });
this.process.on('exit', (code: number) => { this.process.on('exit', (code: number) => {
console.log(`Tlv Storage processor exited with code ${code}`); this.log(ERROR, `Tlv Storage processor exited with code ${code}`);
this.isConnected = false; this.isConnected = false;
if (!code) {
return
}
throw new Error(`Tlv Storage processor exited with code ${code}`)
}); });
this.isConnected = true; this.isConnected = true;