Skip to content

Conversation

@karsonto
Copy link
Member

Fixes #4695.

Motivation

Explain the content here.
Explain why you want to make the changes and what problem you're trying to solve.

Modifications

Describe the modifications you've done.

Documentation

  • Does this pull request introduce a new feature? (yes / no)
  • If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented)
  • If a feature is not applicable for documentation, explain why?
  • If a feature is not documented yet in this PR, please create a followup issue for adding the documentation

@codecov
Copy link

codecov bot commented Dec 29, 2023

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Comparison is base (bd2aeb9) 17.39% compared to head (f51e55e) 17.39%.

Files Patch % Lines
.../apache/eventmesh/client/tcp/common/TcpClient.java 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##             master    #4696   +/-   ##
=========================================
  Coverage     17.39%   17.39%           
  Complexity     1759     1759           
=========================================
  Files           797      797           
  Lines         29850    29850           
  Branches       2579     2579           
=========================================
  Hits           5192     5192           
  Misses        24177    24177           
  Partials        481      481           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

contexts.put(key, context);
} else {
LogUtils.info(log, "duplicate key : {}", key);
synchronized (context) {
Copy link
Member

Choose a reason for hiding this comment

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

Using context as a lock object does not seem to achieve thread safety, as context will be different objects in concurrent situations.
Perhaps ConcurrentHashMap#putIfAbsent() is one of the optional solutions.

Copy link
Member Author

Choose a reason for hiding this comment

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

Really exist the above-mentioned problem , It has been recommitted for review. Thank you.

Comment on lines 184 to 189
synchronized (context) {
if (!contexts.containsValue(context)) {
contexts.put(key, context);
} else {
LogUtils.info(log, "duplicate key : {}", key);
}
Copy link
Member

@Pil0tXia Pil0tXia Dec 29, 2023

Choose a reason for hiding this comment

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

When the client sends two identical messages due to network issues, the existing logic can avoid duplicate keys. However, if the client sends two messages with the same content but different seq due to business issues, the existing logic, when running in a thread-unsafe manner, will retain the key and message from the first received package and will not be able to output a duplicate key log. Therefore, it is still necessary to use putIfAbsent.

Suggested change
synchronized (context) {
if (!contexts.containsValue(context)) {
contexts.put(key, context);
} else {
LogUtils.info(log, "duplicate key : {}", key);
}
RequestContext previousContext = contexts.putIfAbsent(key, context);
if (previousContext != null) {
LogUtils.info(log, "duplicate key : {}", key);
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Please review again, thanks

Copy link
Member

@Pil0tXia Pil0tXia left a comment

Choose a reason for hiding this comment

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

LGTM


package org.apache.eventmesh.client.tcp.common;

import java.util.Objects;
Copy link
Member

Choose a reason for hiding this comment

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

Please remove redundant import for successful CI.

Copy link
Member Author

Choose a reason for hiding this comment

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

ok,please review again

import org.apache.eventmesh.common.protocol.tcp.Package;
import org.apache.eventmesh.common.protocol.tcp.UserAgent;
import org.apache.eventmesh.common.protocol.tcp.codec.Codec;
import org.apache.eventmesh.common.utils.LogUtils;
Copy link
Member

Choose a reason for hiding this comment

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

CI will report imports order error after this modification. You can refer to https://eventmesh.apache.org/community/contribute/contribute#code-style to help check checkstyle.

pandaapo
pandaapo previously approved these changes Jan 2, 2024
@xwm1992 xwm1992 merged commit 3642be0 into apache:master Jan 3, 2024
@xwm1992 xwm1992 added this to the 1.11.0 milestone Dec 18, 2024
xuhongjia pushed a commit to Deckers-Ohana/eventmesh that referenced this pull request Mar 13, 2025
* fix tcp client thread not safe

* fix bug

* fix bug

* fix bug

* fix bug

* fix bug

* fix bug

* fix style problem

* fix style problem

* fix style problem
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.

[Bug] TcpClient not thread safety.

4 participants