Make WordPress Core

Opened 6 months ago

Last modified 6 weeks ago

#65171 new defect (bug)

wp_check_post_lock_window filter values below 120s break post lock detection in backgrounded tabs

Reported by: katag9k Owned by:
Priority: normal Milestone: Awaiting Review
Component: Editor Version:
Severity: normal Keywords: has-patch needs-unit-tests needs-testing
Cc: Focuses: ui, javascript

Description

Summary

When wp_check_post_lock_window is filtered to a value lower than the maximum heartbeat interval (120s), post lock detection silently breaks for users editing in backgrounded tabs. A second editor opening the same post sees no "Currently being edited" modal, walks into the editor, and on the next heartbeat takes over the lock — silently overwriting any unsaved changes the first editor had typed.

Steps to reproduce

Add the following to a theme or plugin:

  add_filter( 'wp_check_post_lock_window', function() { return 30; } );
  1. User A opens /wp-admin/post.php?post=N&action=edit. _edit_lock is set with the current timestamp.
  2. User A switches to a different tab/window so the editor tab is backgrounded. heartbeat.js overrides interval to 120000ms.
  3. After 30 seconds, the lock is considered expired by wp_check_post_lock() (uses the filtered window).
  4. User A's lock is not refreshed until 120s elapse (next backgrounded heartbeat).
  5. Between t=30s and t=120s, user B opens the same post. wp_check_post_lock() returns false. No takeover modal appears. B walks into the editor.
  6. B's first heartbeat claims the lock. A's next heartbeat receives lock_error and A is shown the takeover modal — losing any unsaved changes.

I reproduced this end-to-end by logging both heartbeat traffic and wp_check_post_lock_window calls. The 120s gap between A's heartbeats is exactly what heartbeat.js:512 produces; the lack of modal for B is exactly what post.php produces when the lock has aged past 30s.

Code paths

The defect is two pieces of core that have no shared awareness:

src/wp-admin/includes/post.php, wp_check_post_lock():

  $time_window = apply_filters( 'wp_check_post_lock_window', 150 );

  if ( $time && $time > time() - $time_window && get_current_user_id() !== $user ) {
      return $user;
  }

The filter has no documented minimum value.

src/js/_enqueues/wp/heartbeat.js, scheduleNextTick():

  if ( ! settings.hasFocus ) {
      interval = 120000; // 120 seconds. Post locks expire after 150 seconds.
  }

The 120000 constant is hardcoded; the comment confirms the design implicitly assumes the lock window is always 150s. There is no mechanism for a customised lock window to reach the JS layer.

src/wp-includes/general-template.php, wp_heartbeat_settings(), does not expose the lock window — it only sets ajaxurl and nonce.

Change History (8)

This ticket was mentioned in ​PR #11732 on ​WordPress/wordpress-develop by ​@sukhendu2002.


6 months ago
#1

  • Keywords has-patch added; needs-patch removed

#2 @adamsilverstein
6 months ago

  • Keywords needs-unit-tests needs-testing added

@sukhendu2002 - the ​patch looks good. It would be nice to add some unit tests, ideally reproducing the issue / fixed by the patch.

@katag9k thanks for opening the ticket. Can you check if the patch from @sukhendu2002 fixes the issue for you?

​@adamsilverstein commented on ​PR #11732:


6 months ago
#3

Thanks for working on this @Sukhendu2002 — nice fix.

To help land this, I drafted PHPUnit coverage for the new post_lock_window setting in wp_heartbeat_set_suspension() on top of your branch (rebased on current trunk so the test file added in r60520 / ​7938204 is present):

​https://github.com/adamsilverstein/wordpress-develop/tree/trac-65171-tests

The single test commit is:
​https://github.com/adamsilverstein/wordpress-develop/commit/2139778d90

It adds three cases to Tests_Admin_Includes_Misc_WpHeartbeatSetSuspension_Test:

  • post_lock_window is exposed as 150 on post.php and post-new.php.
  • post_lock_window is omitted on non-post screens (index.php, edit.php).
  • post_lock_window honors the wp_check_post_lock_window filter (verified with a custom value of 60).

Locally: OK (9 tests, 11 assertions).

Feel free to fold these into this PR if you'd like — happy to push to your branch directly if that's easier. Let me know.

​@sukhendu2002 commented on ​PR #11732:


6 months ago
#4

Thanks @adamsilverstein! I've cherry-picked your test commit into my branch. All tests passing. Appreciate the coverage!

#5 @bismarehman
4 months ago

I added the following filter:

`php
add_filter( 'wp_check_post_lock_window', function() {

return 30;

} );

`

I created two users and logged them in using separate browsers.

User A opened a post for editing, made changes without saving, and left the editor open in a background tab. After waiting, User B opened the same post.

Result: User B was able to open the post directly in the editor without seeing the "This post is already being edited" modal. After User B acquired the lock, User A received the "Someone else has taken over this post" notice.

I was able to reproduce the reported issue on my local setup.

#6 @luisdavid01
4 months ago

## Patch Testing Report

Patch tested: ​https://github.com/WordPress/wordpress-develop/pull/11732

### Environment

WordPress: 7.1-alpha-62161-src
PHP: 8.3.32
Server: nginx 1.31.2
Database: mysql 9.7.1
Browser: Helium 0.14.3.1 (Official Build, Chromium 150.0.7871.46) Arch Linux (64-bit)
OS: Operating System CachyOS 7.1.3
Theme: twentytwentyfive 1.5
MU Plugins:

  • None

Plugins:

  • Hello Dolly 1.7.2

### Steps taken

  1. User A opens a post to edit
  2. Open another tab and wait ~30s
  3. User B enters the same post
  4. User B is shown that User A is currently editing the post

✅ Patch is solving the problem: The patch fixes the issue with php and hearthbeat.js not being in sync when a custom wp_check_post_lock_window filter is being used.

### Expected result
The user editing in a background task lock shouldn't be given away during the 30s-120s window refresh lock allowing a second actor to edit without notice.
### Additional Notes

#7 @vedantere
3 months ago

Patch tested: ​https://github.com/WordPress/wordpress-develop/pull/11732(​https://github.com/WordPress/wordpress-develop/pull/11732) (fix/post-lock-heartbeat-background-tab-race @ 512d4ef417)

Tested on trunk with PHP 8.3.31 and MariaDB in the project Docker env. I did code review plus automated tests

The change looks correct: PHP and JS now use the same wp_check_post_lock_window value instead of separate assumptions, and the background-tab heartbeat interval now adjusts safely for shorter lock windows. The default behavior stays unchanged at 120s.

Checks passed:
vendor/bin/phpunit --filter WpHeartbeatSetSuspension_Test tests/phpunit/tests/admin/includes/misc/WpHeartbeatSetSuspension_Test.php
vendor/bin/phpunit tests/phpunit/tests/admin/includes/misc/
vendor/bin/phpunit --group ajax tests/phpunit/tests/ajax/wpAjaxHeartbeat.php

wp-scripts lint-js still shows a lot of pre-existing noise in heartbeat.js; the only new item is a cosmetic Prettier wrap nit.

No regressions found.

#8 @jamesbregenzer
6 weeks ago

Tested PR #11732 at 512d4ef41768d02b9f8befd6727e242cbe172232 against 64ede28c5d781d55cf017ca3f6f744da00e52bc8.
This independently confirms the current behavior with a separate test pass. Required build profile completed.
Relevant checks:

  • Required build profile completed.
  • Focused PHPUnit profile completed.
  • browser.wordpress-admin completed with.
Note: See TracTickets for help on using tickets.