diff --git a/packages/core/src/network/persistent-connection.ts b/packages/core/src/network/persistent-connection.ts index 26f732a6..fe652fca 100644 --- a/packages/core/src/network/persistent-connection.ts +++ b/packages/core/src/network/persistent-connection.ts @@ -5,7 +5,7 @@ import type { IPacketCodec, ITelegramConnection, TelegramTransport } from './tra import { FramedReader, FramedWriter } from '@fuman/io' -import { PersistentConnection as FumanPersistentConnection } from '@fuman/net' +import { ConnectionClosedError, PersistentConnection as FumanPersistentConnection } from '@fuman/net' import { Emitter, timers } from '@fuman/utils' export interface PersistentConnectionParams { @@ -140,7 +140,7 @@ export abstract class PersistentConnection { } } - private async _onClose() { + private _onClose(): void { this.log.debug('connection closed') this._updateLogPrefix() @@ -149,9 +149,12 @@ export abstract class PersistentConnection { this.onClosed() } - private async _onError(err: Error) { + private _onError(err: Error) { this._updateLogPrefix() - this.handleError(err) + + if (!(err instanceof ConnectionClosedError)) { + this.handleError(err) + } return 'reconnect' as const } @@ -231,7 +234,13 @@ export abstract class PersistentConnection { if (this._writer) { this._rescheduleInactivity() - await this._writer.write(data) + try { + await this._writer.write(data) + } catch (e: unknown) { + this.log.warn('encountered an error closed while sending, reconnecting: %e', e) + this._fuman.reconnect(true) + this._sendOnceConnected.push(data) + } } else { this._sendOnceConnected.push(data) } diff --git a/packages/core/src/network/session-connection.ts b/packages/core/src/network/session-connection.ts index 04bac021..e923dfd0 100644 --- a/packages/core/src/network/session-connection.ts +++ b/packages/core/src/network/session-connection.ts @@ -10,8 +10,8 @@ import { Deferred, Emitter, timers, u8 } from '@fuman/utils' import { tl } from '@mtcute/tl' import { TlBinaryReader, TlBinaryWriter, TlSerializationCounter } from '@mtcute/tl-runtime' import Long from 'long' -import { MtArgumentError, MtcuteError, MtTimeoutError } from '../types/index.js' +import { MtArgumentError, MtcuteError, MtTimeoutError } from '../types/index.js' import { createAesIgeForMessageOld } from '../utils/crypto/mtproto.js' import { EarlyTimer, @@ -2050,18 +2050,7 @@ export class SessionConnection extends PersistentConnection { ) const enc = this._session.encryptMessage(result) - const promise = this.send(enc).catch((err: Error) => { - if (this._destroyed) return - this.log.error('error while sending pending messages (root msg_id = %l): %e', rootMsgId, err) - - // put acks in the front so they are the first to be sent - if (ackMsgIds) { - this._session.queuedAcks.splice(0, 0, ...ackMsgIds) - } - if (rootMsgId) { - this._onMessageFailed(rootMsgId, 'unknown error') - } - }) + const promise = this.send(enc) if (this._inactivityPendingFlush && !this._session.hasPendingMessages) { void promise.then(() => {