Repository navigation
Conversation
|
why |
When underlying client retries it takes updated entities and crashes because entities refers text that is not in text: |
|
Why would you mutate the entities after passing it to the plugin? Most bots don't do this, and then this fix it pointless. And if we want to truly guard against arbitrary mutations we have to deep clone the array. All you did is submit a partial fix for some cases. Also, if you really need to mutate things for some reason, why not copy the array yourself? |
|
Reproduce: const { Bot } = require("grammy");
const { autoRetry } = require("@grammyjs/auto-retry");
const { stream } = require("@grammyjs/stream");
const { readFile } = require('node:fs/promises');
const bot = new Bot("");
bot.api.config.use(autoRetry());
bot.use(stream());
async function* slowText() {
for (let i = 0; i < 8192; i += 32) {
yield {text: ".".repeat(31) + "\n", entities: [{type: "code", offset: i, length: 32}]};
await new Promise((r) => setTimeout(r, 100));
}
}
// Telegram only supports streaming in private chats.
bot.chatType("private")
.command("stream", async (ctx) => {
// Stream the message!
await ctx.replyWithStream(slowText());
});
bot.start();I get: GrammyError: Call to 'sendMessageDraft' failed! (400: Bad Request: entity begins after the end of the text at UTF-16 offset 736)
at toGrammyError (/home/azazkamaz/Code/tgclaw/node_modules/grammy/out/core/error.js:47:12)
at ApiClient.callApi (/home/azazkamaz/Code/tgclaw/node_modules/grammy/out/core/client.js:100:48)
at process.processTicksAndRejections (node:internal/process/task_queues:103:5)
at async push (/home/azazkamaz/Code/tgclaw/node_modules/@grammyjs/stream/out/stream.js:142:29)
at async Promise.all (index 1)
at async streamMessage (/home/azazkamaz/Code/tgclaw/node_modules/@grammyjs/stream/out/stream.js:160:13)
at async Context.streamMessage [as replyWithStream] (/home/azazkamaz/Code/tgclaw/node_modules/@grammyjs/stream/out/stream.js:32:20)
at async /home/azazkamaz/Code/tgclaw/test3.cjs:22:5
at async /home/azazkamaz/Code/tgclaw/node_modules/grammy/out/composer.js:578:13
at async /home/azazkamaz/Code/tgclaw/node_modules/grammy/out/composer.js:62:13 {
error: GrammyError: Call to 'sendMessageDraft' failed! (400: Bad Request: entity begins after the end of the text at UTF-16 offset 736)
at toGrammyError (/home/azazkamaz/Code/tgclaw/node_modules/grammy/out/core/error.js:47:12)
at ApiClient.callApi (/home/azazkamaz/Code/tgclaw/node_modules/grammy/out/core/client.js:100:48)
at process.processTicksAndRejections (node:internal/process/task_queues:103:5)
at async push (/home/azazkamaz/Code/tgclaw/node_modules/@grammyjs/stream/out/stream.js:142:29)
at async Promise.all (index 1)
at async streamMessage (/home/azazkamaz/Code/tgclaw/node_modules/@grammyjs/stream/out/stream.js:160:13)
at async Context.streamMessage [as replyWithStream] (/home/azazkamaz/Code/tgclaw/node_modules/@grammyjs/stream/out/stream.js:32:20)
at async /home/azazkamaz/Code/tgclaw/test3.cjs:22:5
at async /home/azazkamaz/Code/tgclaw/node_modules/grammy/out/composer.js:578:13
at async /home/azazkamaz/Code/tgclaw/node_modules/grammy/out/composer.js:62:13 {
method: 'sendMessageDraft',
payload: {
chat_id: 218061276,
draft_id: 173078985984,
text: '...............................\n' +
'...............................\n' +
'...............................\n' +
'...............................\n' +
'...............................\n' +
'...............................\n' +
'...............................\n' +
'...............................\n' +
'...............................\n' +
'...............................\n' +
'...............................\n' +
'...............................\n' +
'...............................\n' +
'...............................\n' +
'...............................\n' +
'...............................\n' +
'...............................\n' +
'...............................\n' +
'...............................\n' +
'...............................\n' +
'...............................\n' +
'...............................\n',
entities: [
{ type: 'code', offset: 0, length: 32 },
{ type: 'code', offset: 32, length: 32 },
{ type: 'code', offset: 64, length: 32 },
{ type: 'code', offset: 96, length: 32 },
{ type: 'code', offset: 128, length: 32 },
{ type: 'code', offset: 160, length: 32 },
{ type: 'code', offset: 192, length: 32 },
{ type: 'code', offset: 224, length: 32 },
{ type: 'code', offset: 256, length: 32 },
{ type: 'code', offset: 288, length: 32 },
{ type: 'code', offset: 320, length: 32 },
{ type: 'code', offset: 352, length: 32 },
{ type: 'code', offset: 384, length: 32 },
{ type: 'code', offset: 416, length: 32 },
{ type: 'code', offset: 448, length: 32 },
{ type: 'code', offset: 480, length: 32 },
{ type: 'code', offset: 512, length: 32 },
{ type: 'code', offset: 544, length: 32 },
{ type: 'code', offset: 576, length: 32 },
{ type: 'code', offset: 608, length: 32 },
{ type: 'code', offset: 640, length: 32 },
{ type: 'code', offset: 672, length: 32 },
{ type: 'code', offset: 704, length: 32 },
{ type: 'code', offset: 736, length: 32 },
{ type: 'code', offset: 768, length: 32 },
{ type: 'code', offset: 800, length: 32 },
{ type: 'code', offset: 832, length: 32 },
{ type: 'code', offset: 864, length: 32 },
{ type: 'code', offset: 896, length: 32 },
{ type: 'code', offset: 928, length: 32 },
{ type: 'code', offset: 960, length: 32 },
{ type: 'code', offset: 992, length: 32 },
{ type: 'code', offset: 1024, length: 32 },
{ type: 'code', offset: 1056, length: 32 },
{ type: 'code', offset: 1088, length: 32 },
{ type: 'code', offset: 1120, length: 32 },
{ type: 'code', offset: 1152, length: 32 },
{ type: 'code', offset: 1184, length: 32 },
{ type: 'code', offset: 1216, length: 32 },
{ type: 'code', offset: 1248, length: 32 },
{ type: 'code', offset: 1280, length: 32 },
{ type: 'code', offset: 1312, length: 32 },
{ type: 'code', offset: 1344, length: 32 },
{ type: 'code', offset: 1376, length: 32 },
{ type: 'code', offset: 1408, length: 32 },
{ type: 'code', offset: 1440, length: 32 },
{ type: 'code', offset: 1472, length: 32 },
{ type: 'code', offset: 1504, length: 32 },
{ type: 'code', offset: 1536, length: 32 },
{ type: 'code', offset: 1568, length: 32 },
{ type: 'code', offset: 1600, length: 32 },
{ type: 'code', offset: 1632, length: 32 },
{ type: 'code', offset: 1664, length: 32 }
]
},
ok: false,
error_code: 400,
description: 'Bad Request: entity begins after the end of the text at UTF-16 offset 736',
parameters: {}
},
name: 'BotError',
ctx: {...}
} |
It is mutated actually in plugin: Line 330 in 0520891 It adds new entities to the list and they get sent on retry of older array because it is passed as ref |
|
Also it seems that my reproduce crashes telegram macos client |
|
Thanks for providing the missing information, let me look into this |
Oof |
|
Ah well, I see it now. Making a shallow copy clearly fixes this, but I'm still not particularly happy with this type of fix. I'll boot up my machine in a bit and try to find a way to fix the concurrency issue without having to duplicate the entire array, such as via double buffering. I'll get back to you |
No description provided.