Retain media request ownership until execution or worker cancellation

This commit is contained in:
evgeny committed 2026-10-29 13:35:41 +01:00
1 parent c9cbcf5ba7
commit 09754d3227
3 files changed
+30 -28

No files matched your search

@@ -11,30 +11,30 @@
int attachment_send(const char* channel_id, const char* src_file_path, const char* db_path,
const uint8_t reply_to[32], uint64_t request_id) {
(void)db_path;
struct UASYNC* ua = instance_lite_get_uasync();
if (!channel_id || !src_file_path || !reply_to || strlen(channel_id) >= 64 || strlen(src_file_path) >= 1024 ||
!uasync_is_running(ua)) {
!instance_lite_is_running()) {
DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "attachment_send: invalid arguments/stopped core"); return -1;
}
const char* name = strrchr(src_file_path, '/'); name = name ? name + 1 : src_file_path;
struct attachment_send_req* req = u_calloc(1, sizeof(*req));
struct posted_task* post = u_calloc(1, sizeof(*post));
if (!req || !post) {
DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "attachment_send: allocation failed"); u_free(req); u_free(post); return -1;
if (!req) {
DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "attachment_send: allocation failed"); return -1;
}
if (strlen(name) >= sizeof(req->info.name)) {
DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "attachment_send: filename too long"); u_free(req); u_free(post); return -1;
DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "attachment_send: filename too long"); u_free(req); return -1;
}
req->inst = instance_lite_get_instance(); req->info.kind = ATTACHMENT_FILE;
snprintf(req->info.name, sizeof(req->info.name), "%s", name);
if (attachment_validate(&req->info)) { u_free(req); u_free(post); return -1; }
if (attachment_validate(&req->info)) { u_free(req); return -1; }
snprintf(req->target, sizeof(req->target), "%s", channel_id);
snprintf(req->source, sizeof(req->source), "%s", src_file_path);
memcpy(req->reply_to, reply_to, sizeof(req->reply_to)); req->request_id = request_id;
req->temporary_source = 1;
DEBUG_INFO(DEBUG_CATEGORY_MEDIA, "attachment_send: accepted channel=%s source=%s request=%llu",
channel_id, src_file_path, (unsigned long long)request_id);
post->callback = attachment_send_trampoline; post->arg = req;
uasync_post_reserved(ua, post);
if (instance_lite_post(attachment_send_trampoline, req, &req->inst, attachment_send_discard)) {
DEBUG_WARN(DEBUG_CATEGORY_MEDIA, "attachment_send: worker rejected request=%llu", (unsigned long long)request_id);
u_free(req); return -1;
}
return 0;
}
@@ -22,8 +22,7 @@ int photo_send(const char* channel_id, const char* src_file_path, const char* db
return -1;
}
struct UASYNC* ua = instance_lite_get_uasync();
if (!uasync_is_running(ua)) { DEBUG_ERROR(DEBUG_CATEGORY_GENERAL, "photo_send: core stopped"); return -1; }
if (!instance_lite_is_running()) { DEBUG_ERROR(DEBUG_CATEGORY_GENERAL, "photo_send: core stopped"); return -1; }
struct stat st; memset(&st, 0, sizeof(st));
if (stat(src_file_path, &st) != 0 || st.st_size <= 0) {
@@ -33,13 +32,12 @@ int photo_send(const char* channel_id, const char* src_file_path, const char* db
if (!strcmp(content_type, "image/jpeg") || !strcmp(content_type, "image/png")) {
struct attachment_send_req* req = u_calloc(1, sizeof(*req));
struct posted_task* post = u_calloc(1, sizeof(*post));
if (!req || !post) { DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "photo_send: allocation failed"); u_free(req); u_free(post); return -1; }
if (!req) { DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "photo_send: allocation failed"); return -1; }
req->inst = instance_lite_get_instance(); req->is_dm = !strncmp(channel_id, "dm:", 3);
const char* target = channel_id + (req->is_dm ? 3 : 0);
const char* name = strrchr(src_file_path, '/'); name = name ? name + 1 : src_file_path;
if (strlen(target) >= sizeof(req->target) || strlen(name) >= sizeof(req->info.name) || (req->is_dm && !generation)) {
DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "photo_send: invalid target/name/generation"); u_free(req); u_free(post); return -1;
DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "photo_send: invalid target/name/generation"); u_free(req); return -1;
}
req->dm_generation = generation; req->temporary_source = 1;
req->info.kind = !strcmp(content_type, "image/png") ? ATTACHMENT_PNG : ATTACHMENT_JPEG;
@@ -47,13 +45,16 @@ int photo_send(const char* channel_id, const char* src_file_path, const char* db
snprintf(req->target, sizeof(req->target), "%s", target);
snprintf(req->source, sizeof(req->source), "%s", src_file_path);
snprintf(req->info.name, sizeof(req->info.name), "%s", name);
if (attachment_validate(&req->info)) { u_free(req); u_free(post); return -1; }
if (attachment_validate(&req->info)) { u_free(req); return -1; }
req->request_id = request_id;
if (!req->is_dm) memcpy(req->reply_to, reply_to, 32);
DEBUG_INFO(DEBUG_CATEGORY_MEDIA, "photo_send: target=%s mime=%s dimensions=%dx%d bytes=%lld generation=%llu",
channel_id, content_type, width, height, (long long)st.st_size, (unsigned long long)generation);
post->callback = attachment_send_trampoline; post->arg = req;
uasync_post_reserved(ua, post); return 0;
if (instance_lite_post(attachment_send_trampoline, req, &req->inst, attachment_send_discard)) {
DEBUG_WARN(DEBUG_CATEGORY_MEDIA, "photo_send: worker rejected request=%llu", (unsigned long long)request_id);
u_free(req); return -1;
}
return 0;
}
if (strncmp(channel_id, "dm:", 3) == 0 ||
(strcmp(content_type, "image/gif") && strcmp(content_type, "image/bmp") && strcmp(content_type, "image/webp"))) {
@@ -99,9 +100,8 @@ int photo_send(const char* channel_id, const char* src_file_path, const char* db
size_t datalen = strlen(original_basename);
struct chat_msg_submit* req = u_calloc(1, sizeof(struct chat_msg_submit) + datalen + 1);
struct posted_task* post = u_calloc(1, sizeof(*post));
if (!req || !post) {
DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "photo_send: allocation failed"); u_free(req); u_free(post); return -1;
if (!req) {
DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "photo_send: allocation failed"); return -1;
}
req->inst = instance_lite_get_instance();
@@ -124,7 +124,9 @@ int photo_send(const char* channel_id, const char* src_file_path, const char* db
channel_id, content_type, src_file_path, media_file, original_basename,
(long long)st.st_size, width, height);
post->callback = chat_core_submit_trampoline; post->arg = req;
uasync_post_reserved(ua, post);
if (instance_lite_post(chat_core_submit_trampoline, req, &req->inst, NULL)) {
DEBUG_WARN(DEBUG_CATEGORY_MEDIA, "photo_send: worker rejected request=%llu", (unsigned long long)request_id);
u_free(req); return -1;
}
return 0;
}
@@ -11,14 +11,12 @@
int video_send(const char* channel_id, const char* src_file_path, const char* db_path,
int duration_ms, int width, int height, const uint8_t reply_to[32], uint64_t request_id, uint64_t generation) {
(void)db_path;
struct UASYNC* ua = instance_lite_get_uasync();
if (!channel_id || !src_file_path || !uasync_is_running(ua) || duration_ms <= 0 || duration_ms > 86400000 ||
if (!channel_id || !src_file_path || !reply_to || !instance_lite_is_running() || duration_ms <= 0 || duration_ms > 86400000 ||
width <= 0 || width > 16384 || height <= 0 || height > 16384 || strlen(src_file_path) >= 1024) {
DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "video send: invalid metadata/target or stopped core"); return -1;
}
struct attachment_send_req* req = u_calloc(1, sizeof(*req));
struct posted_task* post = u_calloc(1, sizeof(*post));
if (!req || !post) { DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "video send: allocation failed"); u_free(req); u_free(post); return -1; }
if (!req) { DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "video send: allocation failed"); return -1; }
req->dm_generation = generation;
req->inst = instance_lite_get_instance(); req->is_dm = !strncmp(channel_id, "dm:", 3);
req->request_id = request_id;
@@ -28,7 +26,9 @@ int video_send(const char* channel_id, const char* src_file_path, const char* db
req->info.kind = ATTACHMENT_VIDEO; req->info.duration_ms = (uint32_t)duration_ms;
req->info.width = (uint16_t)width; req->info.height = (uint16_t)height;
req->temporary_source = 1; /* CameraX и picker создают принадлежащие приложению cache-файлы. */
post->callback = attachment_send_trampoline; post->arg = req;
uasync_post_reserved(ua, post);
if (instance_lite_post(attachment_send_trampoline, req, &req->inst, attachment_send_discard)) {
DEBUG_WARN(DEBUG_CATEGORY_MEDIA, "video send: worker rejected request=%llu", (unsigned long long)request_id);
u_free(req); return -1;
}
return 0;
}