Skip to content

fix: Ensure that message being sent is added to the bottom (#8027)#8085

Merged
iequidoo merged 1 commit intomainfrom
iequidoo/outgoing_msg_after_another_from_future
Apr 13, 2026
Merged

fix: Ensure that message being sent is added to the bottom (#8027)#8085
iequidoo merged 1 commit intomainfrom
iequidoo/outgoing_msg_after_another_from_future

Conversation

@iequidoo
Copy link
Copy Markdown
Collaborator

@iequidoo iequidoo commented Apr 4, 2026

Before, if the user fixed their clock incorrectly set to the future, they needed to delete
previously sent messages or wait until this future comes again so that new sent messages are added
to the bottom. Strictly speaking, the problem isn't fixable because we don't know if messages were
incorrectly timestamped into the future or they are timestamped correctly and the clock is now
incorrectly set to the past. Anyway, adding messages to the middle of the chat isn't a good way to
inform the user about the problem.
Fix #8027

@link2xt
Copy link
Copy Markdown
Collaborator

link2xt commented Apr 4, 2026

This does not look like a fixable bug because you don't know if the message was incorrectly timestamped into the future or the message is timestamped correctly and the clock is now incorrectly set to the past.

This is also not what was reported in #8027. Inventing theoretical cases and fixing them one by one will just result in more complicated logic and more complicated bugs, we tried this with memberlist management and eventually had to throw away all the old logic and redesign it from scratch.

@iequidoo
Copy link
Copy Markdown
Collaborator Author

iequidoo commented Apr 5, 2026

This does not look like a fixable bug because you don't know if the message was incorrectly timestamped into the future or the message is timestamped correctly and the clock is now incorrectly set to the past.

Strictly speaking, yes, but for incoming messages this is actually fixed by tweak_sort_timestamp() and i don't understand why for outgoing messages it should be different. This means that tweak_sort_timestamp() is doing the wrong thing because it doesn't limit the message sort timestamp with smeared time. I.e. smth should be fixed to provide consistent behavior for incoming and outgoing messages.

This is also not what was reported in #8027.

In this issue the user set their clock to the past by mistake and was experiencing sent messages added to the middle of the chat. It's not much different from setting the clock to the future by mistake and then fixing it back.

@iequidoo iequidoo changed the title test: Message sent from future shall not stick to the bottom (#8027) fix: Ensure that message being sent is added to the bottom (#8027) Apr 5, 2026
@iequidoo iequidoo marked this pull request as ready for review April 5, 2026 15:36
@iequidoo
Copy link
Copy Markdown
Collaborator Author

iequidoo commented Apr 5, 2026

If there is a reason why we sort received messages to the bottom (even if the user set their clock to the past), but we shouldn't do this for sent messages, this PR should be closed, but the reason should be documented as a comment at least.

Copy link
Copy Markdown
Collaborator

@link2xt link2xt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's merge this, it is a bit unfortunate that this adds a rather complicated SQL query but likely fixes the case of active chatting between senders that have clocks desynced by ~10 seconds or so which is somewhat expected (#8088 (comment)).

UPDATE msgs SET
timestamp=(
SELECT MAX(timestamp) FROM msgs WHERE
state IN(10,13,16,18,20,24,26,28) AND
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add comments like

10, -- InFresh
13, -- InNoticed
...
28, -- OutMdnRcvd

and say that OutDraft is excluded? It is not really possible to see what is happening here without cross-referencing to MessageState and checking what each number means.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a shorter comment. I think it should be enough, the only reason why message states are explicitly enumerated is making the index work

@iequidoo
Copy link
Copy Markdown
Collaborator Author

iequidoo commented Apr 10, 2026

[...] but likely fixes the case of active chatting between senders that have clocks desynced by ~10 seconds or so which is somewhat expected (#8088 (comment)).

I couldn't understand how this fixes the referenced problem. The comment says:

[...] despite you've already sent your message and the client has rendered it, the friend's message (when it is received) suddenly appears above yours.

But this PR only adjusts sent messages to the bottom.

This PR can fix things if a sent message is rendered slowly e.g. because it's big, so before this PR it could appear in the chat above messages just received.

Before, if the user fixed their clock incorrectly set to the future, they needed to delete
previously sent messages or wait until this future comes again so that new sent messages are added
to the bottom. Strictly speaking, the problem isn't fixable because we don't know if messages were
incorrectly timestamped into the future or they are timestamped correctly and the clock is now
incorrectly set to the past. Anyway, adding messages to the middle of the chat isn't a good way to
inform the user about the problem.
@iequidoo iequidoo force-pushed the iequidoo/outgoing_msg_after_another_from_future branch from d6f8e30 to db7b7be Compare April 10, 2026 17:39
@iequidoo iequidoo requested a review from link2xt April 10, 2026 18:02
@link2xt
Copy link
Copy Markdown
Collaborator

link2xt commented Apr 11, 2026

I couldn't understand how this fixes the referenced problem.

I agree, I thought about the problem the other way round, where your message is sent and appears above, but this cannot actually happen just during active chatting if your clock does not jump back because all already received messages must be in the past as we normally cap the timestamps to not be in the future.

@iequidoo iequidoo merged commit ad7f873 into main Apr 13, 2026
31 checks passed
@iequidoo iequidoo deleted the iequidoo/outgoing_msg_after_another_from_future branch April 13, 2026 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected message order when system clock set to the past

2 participants