harbor run -t openthoughts/tasktrove-inferredbugs-sandboxes-verifier-inferredbugs-3645harbor download openthoughts/tasktrove-inferredbugs-sandboxes-verifier-inferredbugs-3645Project: NettyGameServer Bug ID: 11 Language: java
Before (Buggy File):
package com.wolf.shoot.service.rpc.server;
import com.wolf.shoot.common.constant.BOEnum;
import com.wolf.shoot.common.constant.GlobalConstants;
import com.wolf.shoot.common.constant.Loggers;
import com.wolf.shoot.common.constant.ServiceName;
import com.wolf.shoot.common.util.FileUtil;
import com.wolf.shoot.common.util.JdomUtils;
import com.wolf.shoot.service.IService;
import com.wolf.shoot.service.rpc.client.RpcConnectManager;
import com.wolf.shoot.service.rpc.client.impl.DbRpcConnnectManngeer;
import com.wolf.shoot.service.rpc.client.impl.GameRpcConnecetMananger;
import com.wolf.shoot.service.rpc.client.impl.WorldRpcConnectManager;
import org.jdom2.Element;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by jwp on 2017/3/8.
* rpc的服务发现
*/
@Service
public class RpcServiceDiscovery implements IService {
private static final Logger LOGGER = Loggers.rpcLogger;
protected Object lock = new Object();
protected List<SdServer> sdWorldServers;
protected List<SdServer> sdGameServers;
protected List<SdServer> sdDbServers;
@Autowired
private WorldRpcConnectManager worldRpcConnectManager;
@Autowired
private GameRpcConnecetMananger gameRpcConnecetMananger;
@Autowired
private DbRpcConnnectManngeer dbRpcConnnectManngeer;
private SdRpcServiceProvider sdRpcServiceProvider;
public void initWorldConnectedServer() throws Exception {
worldRpcConnectManager.initManager();
worldRpcConnectManager.initServers(sdWorldServers);
}
public void initGameConnectedServer() throws Exception {
gameRpcConnecetMananger.initManager();
gameRpcConnecetMananger.initServers(sdGameServers);
}
public void initDbConnectServer() throws Exception{
dbRpcConnnectManngeer.initManager();
dbRpcConnnectManngeer.initServers(sdDbServers);
}
@Override
public String getId() {
return ServiceName.RpcServiceDiscovery;
}
@Override
public void startup() throws Exception {
init();
}
@Override
public void shutdown() throws Exception {
worldRpcConnectManager.stop();
gameRpcConnecetMananger.stop();
dbRpcConnnectManngeer.stop();
}
@SuppressWarnings("unchecked")
public void init() throws Exception {
Element rootElement = JdomUtils.getRootElemet(FileUtil.getConfigURL(GlobalConstants.ConfigFile.RPC_SERVER_CONFIG).getFile());
Map<Integer, SdServer> serverMap = new HashMap<>();
List<SdServer> sdWorldServers = new ArrayList<SdServer>();
Element element = rootElement.getChild(BOEnum.WORLD.toString().toLowerCase());
List<Element> childrenElements = element.getChildren("server");
for (Element childElement : childrenElements) {
SdServer sdServer = new SdServer();
sdServer.load(childElement);
sdWorldServers.add(sdServer);
}
List<SdServer> sdGameServers = new ArrayList<SdServer>();
element = rootElement.getChild(BOEnum.GAME.toString().toLowerCase());
childrenElements = element.getChildren("server");
for (Element childElement : childrenElements) {
SdServer sdServer = new SdServer();
sdServer.load(childElement);
sdGameServers.add(sdServer);
}
List<SdServer> sdDbServers = new ArrayList<SdServer>();
element = rootElement.getChild(BOEnum.DB.toString().toLowerCase());
childrenElements = element.getChildren("server");
for (Element childElement : childrenElements) {
SdServer sdServer = new SdServer();
sdServer.load(childElement);
sdDbServers.add(sdServer);
}
synchronized (this.lock) {
this.sdWorldServers = sdWorldServers;
this.sdGameServers = sdGameServers;
this.sdDbServers = sdDbServers;
}
initWorldConnectedServer();
initGameConnectedServer();
initDbConnectServer();
SdRpcServiceProvider sdRpcServiceProvider = new SdRpcServiceProvider();
rootElement = JdomUtils.getRootElemet(FileUtil.getConfigURL(GlobalConstants.ConfigFile.RPC_SERVEICE_CONFIG).getFile());
childrenElements = rootElement.getChildren("service");
for (Element childElement : childrenElements) {
sdRpcServiceProvider.load(childElement);
}
synchronized (this.lock) {
this.sdRpcServiceProvider = sdRpcServiceProvider;
}
}
public RpcConnectManager getRpcConnectMannger(BOEnum boEnum){
RpcConnectManager rpcConnectManager = worldRpcConnectManager;
if(boEnum.equals(BOEnum.GAME)){
rpcConnectManager = gameRpcConnecetMananger;
}else if (boEnum.equals(BOEnum.DB)){
rpcConnectManager = dbRpcConnnectManngeer;
}
return worldRpcConnectManager;
}
public SdRpcServiceProvider getSdRpcServiceProvider() {
return sdRpcServiceProvider;
}
public void setSdRpcServiceProvider(SdRpcServiceProvider sdRpcServiceProvider) {
this.sdRpcServiceProvider = sdRpcServiceProvider;
}
}
PROVER
ERROR
THREAD_SAFETY_VIOLATION
Read/Write race. Non-private method com.wolf.shoot.service.rpc.server.RpcServiceDiscovery.initDbConnectServer indirectly reads with synchronization from this.com.wolf.shoot.service.rpc.server.RpcServiceDiscovery.dbRpcConnnectManngeer.com.wolf.shoot.service.rpc.client.RpcConnectManager.threadPoolExecutor. Potentially races with unsynchronized write in method RpcServiceDiscovery.init().
Reporting because another access to the same memory occurs on a background thread, although this access may not.
HIGH
user
void RpcServiceDiscovery.initDbConnectServer()
com.wolf.shoot.service.rpc.server.RpcServiceDiscovery.initDbConnectServer():void.67ae69e668ada1210a0d1551dc4bb235
src/main/java/com/wolf/shoot/service/rpc/server/RpcServiceDiscovery.java
RpcServiceDiscovery.java|initDbConnectServer|THREAD_SAFETY_VIOLATION
9c5d6d9028928346cc4fb44cced5dea1
12f1cc8f9fdcf1c6e89e5e30609eb099
Thread Safety Violation
Fix the bug in the code.
Write the complete, corrected version of the file to: /app/src/main/java/com/wolf/shoot/service/rpc/server/RpcServiceDiscovery.java.
The directory does not exist yet. You must create it and write the file:
mkdir -p /app/src/main/java/com/wolf/shoot/service/rpc/server
cat > /app/src/main/java/com/wolf/shoot/service/rpc/server/RpcServiceDiscovery.java << 'ENDOFFILE'
<full corrected file content here>
ENDOFFILE