Skip to content
This repository has been archived by the owner on Aug 23, 2020. It is now read-only.

Fix file descriptors leak: ScriptException is not the only propagated… #1669

Merged
merged 1 commit into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/iota/iri/IRI.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* IRI implements all the core functionality necessary for participating in an IOTA network as a full node.
* This includes, but is not limited to:
* <ul>
* <li>Receiving and broadcasting transactions through TCP and UDP.</li>
* <li>Receiving and broadcasting transactions through TCP</li>
* <li>Handling of HTTP requests from clients.</li>
* <li>Tracking and validating Milestones.</li>
* <li>Loading custom modules that extend the API.</li>
Expand Down
53 changes: 20 additions & 33 deletions src/main/java/com/iota/iri/IXI.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,19 +211,14 @@ private void loadModule(Path modulePath) {
return;
}
Map packageJson;
Reader packageJsonReader;
try {
packageJsonReader = new FileReader(packageJsonPath.toFile());

try (Reader packageJsonReader = new FileReader(packageJsonPath.toFile()) ){
packageJson = gson.fromJson(packageJsonReader, Map.class);
} catch (FileNotFoundException e) {
} catch (IOException e) {
log.error("Could not load {}", packageJsonPath);
return;
}
try {
packageJsonReader.close();
} catch (IOException e) {
log.error("Could not close file {}", packageJsonPath);
}

if(packageJson != null && packageJson.get("main") != null) {
log.info("Loading module: {}", getModuleName(modulePath, true));
Path pathToMain = Paths.get(modulePath.toString(), (String) packageJson.get("main"));
Expand All @@ -242,33 +237,25 @@ private void unloadModule(Path moduleNamePath) {
}

private void attach(Path pathToMain, String moduleName) {
Reader ixiModuleReader;
try {
ixiModuleReader = new FileReader(pathToMain.toFile());
} catch (FileNotFoundException e) {
log.error("Could not load {}", pathToMain);
return;
}
log.info("Starting script: {}", pathToMain);
Map<String, CallableRequest<AbstractResponse>> ixiMap = new HashMap<>();
Map<String, Runnable> startStop = new HashMap<>();
try (Reader ixiModuleReader = new FileReader(pathToMain.toFile())) {
log.info("Starting script: {}", pathToMain);
Map<String, CallableRequest<AbstractResponse>> ixiMap = new HashMap<>();
Map<String, Runnable> startStop = new HashMap<>();

Bindings bindings = scriptEngine.createBindings();
bindings.put("API", ixiMap);
bindings.put("IXICycle", startStop);
bindings.put("IOTA", iota);
Bindings bindings = scriptEngine.createBindings();
bindings.put("API", ixiMap);
bindings.put("IXICycle", startStop);
bindings.put("IOTA", iota);

ixiAPI.put(moduleName, ixiMap);
ixiLifetime.put(moduleName, startStop);
try {
scriptEngine.eval(ixiModuleReader, bindings);
} catch (ScriptException e) {
log.error("Script error", e);
}
try {
ixiModuleReader.close();
ixiAPI.put(moduleName, ixiMap);
ixiLifetime.put(moduleName, startStop);
try {
scriptEngine.eval(ixiModuleReader, bindings);
} catch (ScriptException e) {
log.error("Script error", e);
}
} catch (IOException e) {
log.error("Could not close {}", pathToMain);
log.error("Could not load {}", pathToMain);
}
}

Expand Down