misskey-dev / misskey

🌎 An interplanetary microblogging platform 🚀
https://misskey-hub.net/
GNU Affero General Public License v3.0
9.84k stars 1.33k forks source link

巨大ファイルが添付されていると照会できない #14449

Open penginn-net opened 4 weeks ago

penginn-net commented 4 weeks ago

💡 Summary

巨大なファイルが添付されているとエラーになる

🥰 Expected Behavior

照会できる

🤬 Actual Behavior

default.ymlのmaxFileSizeの値以上だと WARN * [download] maxSize exceeded (2893111290 > 262144000) on response が出た後にINTERNAL_ERROR "Premature close" 書き換えるとダウンロードに間に合わなくてTimeout

📝 Steps to Reproduce

1.ファイルサイズ大きいファイルが添付された投稿を照会する

💻 Frontend Environment

* Model and OS of the device(s):
* Browser:
* Server URL:
* Misskey:

🛰 Backend Environment (for server admin)

* Installation Method or Hosting Service:
* Misskey:2024.8.0
* Node:
* PostgreSQL:
* Redis:
* OS and Architecture:

Do you want to address this bug yourself?

samunohito commented 4 weeks ago

自鯖で再現させてログを採取しました(連合先は身内鯖なのでURLをマスクしています)

 8月 21 20:46:07 misskey[2705654]: INFO 1        [remote ap]        Create: https://example.com/activity
 8月 21 20:46:07 misskey[2705654]: INFO 1        [remote ap]        Creating the Note: https://example.com/xxxxxxxxxxxxx
 8月 21 20:46:07 misskey[2705654]: INFO 1        [remote ap]        Creating the Image: https://example.com/files/xxxxx-xxxxx-xxxxx-xxxxx
 8月 21 20:46:07 misskey[2705654]: INFO 1        [download]        Downloading https://example.com/files/xxxxx-xxxxx-xxxxx-xxxxx to /tmp/tmp-2705654-RnWCYcR7KWx7 ...
 8月 21 20:46:09 misskey[2705654]: WARN 1        [download]        maxSize exceeded (2092370 > 262144) on response
 8月 21 20:46:09 misskey[2705654]: ERR  1        [drive downloader]        Failed to create drive file: Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close {
 8月 21 20:46:09 misskey[2705654]:   url: 'https://example.com/files/xxxxx-xxxxx-xxxxx-xxxxx',
 8月 21 20:46:09 misskey[2705654]:   e: Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close
 8月 21 20:46:09 misskey[2705654]:       at Request.onclose (node:internal/streams/end-of-stream:154:30)
 8月 21 20:46:09 misskey[2705654]:       at Request.emit (node:events:531:35)
 8月 21 20:46:09 misskey[2705654]:       at emitCloseNT (node:internal/streams/destroy:147:10)
 8月 21 20:46:09 misskey[2705654]:       at process.processTicksAndRejections (node:internal/process/task_queues:81:21) {
 8月 21 20:46:09 misskey[2705654]:     code: 'ERR_STREAM_PREMATURE_CLOSE'
 8月 21 20:46:09 misskey[2705654]:   }
 8月 21 20:46:09 misskey[2705654]: }
samunohito commented 4 weeks ago

ログの流れ的に以下の順序で呼ばれているものと思われます

https://github.com/misskey-dev/misskey/blob/93fc06d18b8520919cdf422675c4102b4851df18/packages/backend/src/core/activitypub/models/ApNoteService.ts#L213-L217https://github.com/misskey-dev/misskey/blob/c7d7da8fc58ace9be6cf3af1040ed3a4b7309064/packages/backend/src/core/activitypub/models/ApImageService.ts#L73-L80https://github.com/misskey-dev/misskey/blob/5c42a0e43931f62490c44e389db893b6bfe9e349/packages/backend/src/core/DriveService.ts#L861https://github.com/misskey-dev/misskey/blob/c1514ce91dc08481a092a789ee37da546cdb4583/packages/backend/src/core/DownloadService.ts#L80-L87

samunohito commented 4 weeks ago

md5やmimetype検出のため、一度/tmp配下にダウンロードする必要があるようで…その過程でアップロード容量上限に抵触すると連合経由でのノート作成処理全体を巻き込んでエラーとなってしまっていると考えます。 もし対策するとしたら、ノートのみを表示し、添付ファイルの表示をあきらめるとかになるのでしょうか… (本文なしファイルのみのノートが来たらとか、考えることが多そう。他に妙案があればよいのですが)

penginn-net commented 4 weeks ago

添付ファイルを取得できない時は添付ファイルを取得できませんでした の様な画像(をアセットに入れて、マイグレでファイル追加して(管理者権限で消せない))を添付ファイルIDにするとかならファイルがあることが分かって 良いのでは無いかと思ったんですけどどうですかね...

上のだとオブジェクトストレージへ行かないので起動時にドライブに決め打ちIDが無ければアップロード?

kozakura913 commented 4 weeks ago

巨大なファイルはmd5の計算諦めてファイル先頭の数bytesだけ読んでmime検出したりcontent-typeヘッダを信用するとか