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

Commit

Permalink
Fix: file descriptors leak in IXI module (#1669)
Browse files Browse the repository at this point in the history
  • Loading branch information
nkaraman authored and Gal Rogozinski committed Nov 21, 2019
1 parent 1a9f506 commit 9dad25d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 34 deletions.
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

0 comments on commit 9dad25d

Please sign in to comment.