# 移动帖子返回502错误网关

**URL:** https://meta.discourse.org/t/moving-posts-returns-502-bad-gateway/75654
**Category:** Self-hosting
**Tags:** performance, server-resources
**Created:** [2017年十二月7日 23:29 UTC](https://meta.discourse.org/t/moving-posts-returns-502-bad-gateway/75654 "2017-12-07T23:29:36Z")
**Posts on this page:** 13
**Page:** 4

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [2020年十一月6日 04:39 UTC](https://meta.discourse.org/t/moving-posts-returns-502-bad-gateway/75654/67 "2020-11-06T04:39:23Z")

</div>

10 篇帖子已合并到现有主题中：[拆分主题、移动或重命名分类时出现 502 错误](https://meta.discourse.org/t/502-errors-when-splitting-topic-moving-or-renaming-categories/164107/17)

---

<div class="post-metadata">

### Author: ![Dmitry\_Krasnoperov](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dmitry_krasnoperov/32/65079_2.png) [@Dmitry\_Krasnoperov](https://meta.discourse.org/u/Dmitry_Krasnoperov)
#### Post date: [2021年二月2日 21:48 UTC](https://meta.discourse.org/t/moving-posts-returns-502-bad-gateway/75654/68 "2021-02-02T21:48:41Z")

</div>

这个烦人的 bug 依然存在。你能给我一些排查问题的步骤吗？

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [2021年五月25日 21:29 UTC](https://meta.discourse.org/t/moving-posts-returns-502-bad-gateway/75654/69 "2021-05-25T21:29:52Z")

</div>

不幸的是，我们偶尔仍会遇到这个问题。我收集了一些关于最近一次失败案例的数据：将 27 篇帖子移动到现有主题（共 2013 篇帖子）时失败了：

```plaintext
Beginning move at 2021-05-25 20:54:57 +0000
create_temp_table
  0.000134 0.000027 0.000161 ( 0.002810)

delete_invalid_post_timings
  0.000317 0.000065 0.000382 ( 0.306869)

move_incoming_emails
  0.000056 0.000006 0.000062 ( 0.000478)

move_notifications
  0.000074 0.000008 0.000082 ( 0.005584)

update_reply_counts
  0.000088 0.000010 0.000098 ( 0.001058)

update_quotes
  0.000079 0.000008 0.000087 ( 0.002426)

move_first_post_replies
  0.000071 0.000008 0.000079 ( 0.000695)

delete_post_replies
  0.000062 0.000006 0.000068 ( 0.000834)

copy_first_post_timings
  0.000064 0.000007 0.000071 ( 0.002660)

move_post_timings
  0.000082 0.000009 0.000091 ( 0.008195)

copy_topic_users
  0.000398 0.000043 0.000441 ( 0.043640)

move_each_post
  4.345220 0.132652 4.477872 ( 3.447047)

create_moderator_post_in_original_topic
  0.163962 0.000253 0.164215 ( 0.207101)

update_statistics
  0.217266 0.051660 0.268926 ( 45.258070)

update_user_actions
  0.000934 0.000101 0.001035 ( 0.178894)

update_last_post_stats
  0.001589 0.000171 0.001760 ( 0.003156)

update_upload_security_status
  0.000030 0.000003 0.000033 ( 0.000034)

update_bookmarks
  0.000492 0.000054 0.000546 ( 0.003832)

Finished move at 2021-05-25 20:55:47 +0000

```

大多数方法都很快完成，但在这种情况下，`update_statistics` 耗时超过 45 秒。所有时间都花在以下方法调用上：

> <https://github.com/discourse/discourse/blob/66d17fdd6b09d95ff5e3e5c06c7447688fa6b79e/app/models/post_mover.rb#L435-L435>

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2021年六月1日 06:55 UTC](https://meta.discourse.org/t/moving-posts-returns-502-bad-gateway/75654/70 "2021-06-01T06:55:24Z")

</div>

也许我们可以将 update\_statistics 的工作延迟到 Sidekiq 处理？

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [2021年六月1日 08:38 UTC](https://meta.discourse.org/t/moving-posts-returns-502-bad-gateway/75654/71 "2021-06-01T08:38:32Z")

</div>

是的，有可能。但我将仔细评估将整个流程迁移到 Sidekiq 作业中并让 UI 等待其完成所需的复杂度。目前我们一直通过将零散部分逐个迁移到作业中来应对问题，这种做法有点像“打地鼠”。

---

<div class="post-metadata">

### Author: ![Dmitry\_Krasnoperov](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dmitry_krasnoperov/32/65079_2.png) [@Dmitry\_Krasnoperov](https://meta.discourse.org/u/Dmitry_Krasnoperov)
#### Post date: [2021年六月20日 07:45 UTC](https://meta.discourse.org/t/moving-posts-returns-502-bad-gateway/75654/72 "2021-06-20T07:45:57Z")

</div>

感谢您的辛勤付出！

---

<div class="post-metadata">

### Author: ![Viktor09](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/viktor09/32/202618_2.png) [@Viktor09](https://meta.discourse.org/u/Viktor09)
#### Post date: [2021年七月6日 13:54 UTC](https://meta.discourse.org/t/moving-posts-returns-502-bad-gateway/75654/73 "2021-07-06T13:54:48Z")

</div>

我在合并话题时也会遇到这个 502 错误。并非每次都会出现，但频率很高。

我这样做是因为有人就一个已存在的话题主题新建了话题，因此通常我只需要移动非常少量的帖子（不超过五条）。

我还没有读完整个话题，但我的理解是这是一个已知问题，你们正在处理，而我自身无法采取任何措施？

---

<div class="post-metadata">

### Author: ![ArcticF0xx](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arcticf0xx/32/179374_2.png) [@ArcticF0xx](https://meta.discourse.org/u/ArcticF0xx)
#### Post date: [2021年七月30日 17:40 UTC](https://meta.discourse.org/t/moving-posts-returns-502-bad-gateway/75654/74 "2021-07-30T17:40:49Z")

</div>

我们在论坛上遇到了同样的问题。我们中有许多人无法编辑和/或创建新帖子。我们收到了以下错误信息：

 ![Discourse_502_error](https://global.discourse-cdn.com/meta/original/3X/7/0/700023ec67ae5638a8f82a149851298eb42c3858.png)

---

<div class="post-metadata">

### Author: ![mentalstring](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mentalstring/32/168934_2.png) [@mentalstring](https://meta.discourse.org/u/mentalstring)
#### Post date: [2021年十一月26日 22:53 UTC](https://meta.discourse.org/t/moving-posts-returns-502-bad-gateway/75654/75 "2021-11-26T22:53:19Z")

</div>

我们还遇到了 502 错误，在移动帖子时发生。

以前只是偶尔发生，并且坚持尝试通常会成功。但现在我们遇到一个案例，在尝试将单个帖子移动到较长的帖子（2k 帖子）时，它会持续失败，并在大约 30 秒后超时。

发生这种情况时，服务器并不特别繁忙，并且可以很好地处理其他活动。在活动较低时尝试移动帖子会产生相同的结果。安全模式没有区别。

除了增强实例（假设这是其中一种解决方案？），还有什么我们可以尝试的吗？如果信息有用，我很乐意提供更多信息。值得一提的是，我使用的是最新的稳定版本——如果最近有针对此问题的更改，我会考虑切换到最新的测试版。

---

<div class="post-metadata">

### Author: ![mentalstring](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mentalstring/32/168934_2.png) [@mentalstring](https://meta.discourse.org/u/mentalstring)
#### Post date: [2021年十一月29日 10:28 UTC](https://meta.discourse.org/t/moving-posts-returns-502-bad-gateway/75654/76 "2021-11-29T10:28:27Z")

</div>

周末（我们最不忙碌的时候）我们暂时将存储最大 IOPS 增加了两倍，然后还将最大吞吐量增加了两倍，但仍然无法将一个帖子移动到包含 2k 个帖子的现有主题中。

在主题本身上发表新回复似乎没有问题，所以我猜想当帖子被移动而不是被重新创建时，会涉及更多额外的工作。如果无法在 30 秒内完成，也许应该通过后台作业来完成，而不是以“502 错误”消息失败？

---

<div class="post-metadata">

### Author: ![Dmitry\_Krasnoperov](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dmitry_krasnoperov/32/65079_2.png) [@Dmitry\_Krasnoperov](https://meta.discourse.org/u/Dmitry_Krasnoperov)
#### Post date: [2021年十二月13日 21:26 UTC](https://meta.discourse.org/t/moving-posts-returns-502-bad-gateway/75654/77 "2021-12-13T21:26:48Z")

</div>

是的，对于超过 3000 个帖子的旧且冗长的主题，我也会遇到同样的问题。  
这仍然很痛苦。

---

<div class="post-metadata">

### Author: ![gassim](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gassim/32/220910_2.png) [@gassim](https://meta.discourse.org/u/gassim)
#### Post date: [2025年一月12日 10:41 UTC](https://meta.discourse.org/t/moving-posts-returns-502-bad-gateway/75654/78 "2025-01-12T10:41:52Z")

</div>

你好

我遇到了类似的问题，无法将一个主题及其所有回复合并到另一个主题帖子中。我正在合并到的主题有 23 个帖子，而我正在移动的主题只有 4 个帖子。

 ![/](https://global.discourse-cdn.com/meta/original/4X/3/4/0/340a10f8c6ae8c66d2f839cb76bcd113ac09d17b.png) | ![/](https://global.discourse-cdn.com/meta/original/4X/f/0/c/f0c4f19889619dbc980a129976c275fe8548986f.png)

> **错误日志**
>
> ```plaintext
> Message (4 copies reported)
> Failed to process hijacked response correctly : PG::UniqueViolation : ERROR: duplicate key value violates unique constraint "post_timings_unique"
> DETAIL: Key (topic_id, post_number, user_id)=(62975, 19, 538) already exists.
> Backtrace
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/rack-mini-profiler-3.3.1/lib/patches/db/pg.rb:110:in `exec'
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/rack-mini-profiler-3.3.1/lib/patches/db/pg.rb:110:in `async_exec'
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/mini_sql-1.6.0/lib/mini_sql/postgres/connection.rb:217:in `run'
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/mini_sql-1.6.0/lib/mini_sql/active_record_postgres/connection.rb:38:in `block in run'
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/mini_sql-1.6.0/lib/mini_sql/active_record_postgres/connection.rb:34:in `block in with_lock'
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activesupport-7.2.2.1/lib/active_support/concurrency/null_lock.rb:9:in `synchronize'
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/mini_sql-1.6.0/lib/mini_sql/active_record_postgres/connection.rb:34:in `with_lock'
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/mini_sql-1.6.0/lib/mini_sql/active_record_postgres/connection.rb:38:in `run'
> /var/www/discourse/lib/mini_sql_multisite_connection.rb:109:in `run'
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/mini_sql-1.6.0/lib/mini_sql/postgres/connection.rb:196:in `exec'
> /var/www/discourse/app/models/post_mover.rb:517:in `move_post_timings'
> /var/www/discourse/app/models/post_mover.rb:142:in `handle_moved_references'
> /var/www/discourse/app/models/post_mover.rb:112:in `move_posts_to'
> /var/www/discourse/app/models/post_mover.rb:35:in `block in to_topic'
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activerecord-7.2.2.1/lib/active_record/connection_adapters/abstract/transaction.rb:616:in `block in within_new_transaction'
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activesupport-7.2.2.1/lib/active_support/concurrency/null_lock.rb:9:in `synchronize'
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activerecord-7.2.2.1/lib/active_record/connection_adapters/abstract/transaction.rb:613:in `within_new_transaction'
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activerecord-7.2.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:361:in `transaction'
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activerecord-7.2.2.1/lib/active_record/transactions.rb:234:in `block in transaction'
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activerecord-7.2.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:415:in `with_connection'
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activerecord-7.2.2.1/lib/active_record/connection_handling.rb:296:in `with_connection'
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/activerecord-7.2.2.1/lib/active_record/transactions.rb:233:in `transaction'
> /var/www/discourse/app/models/post_mover.rb:35:in `to_topic'
> /var/www/discourse/app/models/topic.rb:1308:in `move_posts'
> /var/www/discourse/app/controllers/topics_controller.rb:883:in `block in merge_topic'
> /var/www/discourse/lib/hijack.rb:64:in `instance_eval'
> /var/www/discourse/lib/hijack.rb:64:in `block in hijack'
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/concurrent-ruby-1.3.4/lib/concurrent-ruby/concurrent/promises.rb:911:in `callback_on_resolution'
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/concurrent-ruby-1.3.4/lib/concurrent-ruby/concurrent/promises.rb:797:in `call_callback'
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/concurrent-ruby-1.3.4/lib/concurrent-ruby/concurrent/promises.rb:803:in `call_callbacks'
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/concurrent-ruby-1.3.4/lib/concurrent-ruby/concurrent/promises.rb:692:in `resolve_with'
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/concurrent-ruby-1.3.4/lib/concurrent-ruby/concurrent/promises.rb:1325:in `resolve'
> /var/www/discourse/lib/scheduler/defer.rb:125:in `block in do_work'
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/rails_multisite-6.1.0/lib/rails_multisite/connection_management/null_instance.rb:49:in `with_connection'
> /var/www/discourse/vendor/bundle/ruby/3.3.0/gems/rails_multisite-6.1.0/lib/rails_multisite/connection_management.rb:21:in `with_connection'
> /var/www/discourse/lib/scheduler/defer.rb:119:in `do_work'
> /var/www/discourse/lib/scheduler/defer.rb:105:in `block (2 levels) in start_thread'
> 
> ```

\*\*注意：\*\*我注意到错误信息说“`already exists`”，但实际上它们是不同的主题。我不知道这是否相关，但在出现此错误之前，我将几个主题及其帖子移动到了我试图合并的这个有 23 个帖子的主题中（就像我对其他主题所做的那样）。

谢谢！

---

<div class="post-metadata">

### Author: ![Moin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/moin/32/554653_2.png) [@Moin](https://meta.discourse.org/u/Moin)
#### Post date: [2025年一月12日 11:54 UTC](https://meta.discourse.org/t/moving-posts-returns-502-bad-gateway/75654/79 "2025-01-12T11:54:33Z")

</div>

您最近几天更新了吗？最近有一个与移动帖子相关的修复程序

> <https://github.com/discourse/discourse/commit/260017de1d663ceec0df9ba9a911f4e2da644fed>
>
> Fixes issue in type difference due to modernization in this commit https://gith…ub.com/discourse/discourse/commit/df9de3022f34c8d2003f364279ff1c3e7dfdaf29

[上一頁](https://meta.discourse.org/t/moving-posts-returns-502-bad-gateway/75654.md?page=3)
