soberhacker / obsidian-telegram-sync

Transfer messages and files from Telegram to Obsidian
GNU Affero General Public License v3.0
304 stars 30 forks source link

Bug: "Error: Premature close" #191

Closed Mikle-Bond closed 1 year ago

Mikle-Bond commented 1 year ago

This happens on every video that is about 3MB (and often on smaller ones). Sometimes this error is returned even after download is complete, and progress bar shows for "delete" operation.

I have skimmed through code, and saw, that the code uses two different approaches:

via node-telegram-bot-api TelegramBot.getFileStream https://github.com/soberhacker/obsidian-telegram-sync/blob/195df71eb8f7a873c44fec20ac6bba79d5012321/src/telegram/bot/message/handlers.ts#L120 via telegram TelegramClient.downloadMedia https://github.com/soberhacker/obsidian-telegram-sync/blob/195df71eb8f7a873c44fec20ac6bba79d5012321/src/telegram/bot/message/handlers.ts#L157-L160

Where the second one being used only on "Too big" errors (I'd assume > 20MB, as per https://core.telegram.org/bots/api#getfile). But apparently getFileStream has no protection from faulty network connections and no retry mechanisms.

soberhacker commented 1 year ago

Hi! Thank you for the detailed report!

Do you know the cause of this error? Do you have an unstable internet connection? What version of the plugin are you using?

Did this happen during Obsidian's start? Or does it occur even if Obsidian is already started and you send only one message?

Mikle-Bond commented 1 year ago

Do you know the cause of this error? Do you have an unstable internet connection?

The internet connection is likely not a problem (but I can't prove it is not). All the Internet services work well on the machine that runs Obsidian.

Did this happen during Obsidian's start? Or does it occur even if Obsidian is already started and you send only one message?

In all cases I have fully started Obsidian. It happens with same probability whether I send messages in pack or one-by-one.

I figured out it has to be the size of the stream. I created several test videos with sizes ~10MB, ~15MB and ~40MB. First two fail as described, about 5/6 attempts fail. The 40MB file uploads every time flawlessly.

I tried to catch this in Obsidian's Dev Console, here's the exception

ERR_STREAM_PREMATURE_CLOSE

Error: Premature close
    at __node_internal_captureLargerStackTrace (node:internal/errors:490:5)
    at new NodeError (node:internal/errors:399:5)
    at PassThrough.onclose (node:internal/streams/end-of-stream:149:30)
    at PassThrough.emit (node:events:525:35)
    at emitCloseNT (node:internal/streams/destroy:132:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:81:21)

Here's what appears in the file

---
created: 2023-08-08T13:25:36+03:00
updated: 2023-08-08T13:29:59+03:00
url: 
---

#

[❌ error while handling file](Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close)

and template I use

---
created: {{creationDate:YYYY-MM-DD}}T{{creationTime:HH:mm:ss}}+03:00
updated: {{messageDate:YYYY-MM-DD}}T{{messageTime:HH:mm:ss}}+03:00
url: {{url1}}
---

# {{content:firstLine}}

{{file}}

{{content:noFirstLine}}

{{replace:\n+=>\n}}

What version of the plugin are you using?

SYSTEM INFO:
Obsidian version: v1.3.7
Installer version: v1.3.5
Operating system: Windows 10 Enterprise LTSC 2019 10.0.17763
Login status: not logged in
Insider build toggle: off
Live preview: off
Legacy editor: off
Base theme: adapt to system
Community theme: Shimmering Focus v3.484
Snippets enabled: 15
Restricted mode: off
Plugins installed: 68
Plugins enabled: 33
1: Vimrc Support v0.9.0
2: Code Editor Shortcuts v1.14.0
3: QR Code Generator Plugin v1.1.0
4: Custom File Explorer sorting v1.8.2
5: Ninja Cursor v0.0.13
6: Style Settings v1.0.6
7: CodeMirror Options v0.9.5
8: Scroll Offset v1.0.4
9: Surfing v0.8.16
10: Jump to link v0.4.4
11: Random To-Do v0.1.3
12: Better MathJax v1.0.1
13: Supercharged Links v0.9.7
14: Local REST API v1.6.0
15: Text Extractor v0.4.6
16: Telegraph Publish v1.2.3
17: Telegram Sync v1.9.0
18: Obsidian42 - BRAT v0.7.0
19: Plugin Update Tracker v1.5.1
20: Cycle through Panes v1.2.1
21: Terminal v3.11.1
22: File Explorer Note Count v1.2.1
23: Cycle In Sidebar v1.0.2
24: QuickShare v1.3.1
25: Settings Search v1.3.8
26: MySnippets v1.2.3
27: Tag Wrangler v0.5.11
28: Folder Note Core v1.3.5
29: Linter v1.19.1
30: Hot Reload v0.1.10
31: Advanced Tables v0.19.1
32: Auto Link Title v1.4.1
33: Longform v2.0.3
soberhacker commented 1 year ago

I hope I have fixed this, try in the last prerelease 1.10.0.Beta

Mikle-Bond commented 1 year ago

Yay, seems working fine on my test cases. Thanks for the quick fix!)