Skip to content

Commit

Permalink
Merge pull request #3 from jenkinsci/bugfix-upload-hang
Browse files Browse the repository at this point in the history
Lazily open file streams to prevent blocking on reads
  • Loading branch information
tylercamp committed Sep 29, 2020
2 parents b8c143b + bf2c810 commit b2a790a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 14 deletions.
18 changes: 4 additions & 14 deletions src/main/java/org/jenkinsci/plugins/codedx/CodeDxPublisher.java
Expand Up @@ -223,14 +223,8 @@ public void perform(


if (sourceAndBinaryZip != null) {

try {
buildOutput.println("Adding source/binary zip...");
toSend.put("Jenkins-SourceAndBinary", sourceAndBinaryZip.read());
} catch (IOException e) {
buildOutput.println("Failed to add source/binary zip.");
}

buildOutput.println("Adding source/binary zip...");
toSend.put("Jenkins-SourceAndBinary", new DeferredFilePathInputStream(sourceAndBinaryZip));
} else {
buildOutput.println("No matching source/binary files.");
}
Expand All @@ -242,12 +236,8 @@ public void perform(
FilePath path = workspace.child(file);

if (path.exists()) {
try {
buildOutput.println("Add tool output file " + path.getRemote() + " to request.");
toSend.put(path.getName(), path.read());
} catch (IOException e) {
buildOutput.println("Failed to add tool output file: " + path);
}
buildOutput.println("Add tool output file " + path.getRemote() + " to request.");
toSend.put(path.getName(), new DeferredFilePathInputStream(path));
} else {
buildOutput.println("Path specified but could not be found: " + path);
}
Expand Down
@@ -0,0 +1,63 @@
package org.jenkinsci.plugins.codedx;

import hudson.FilePath;

import java.io.IOException;
import java.io.InputStream;

public class DeferredFilePathInputStream extends InputStream {
FilePath fp;
InputStream is;

public DeferredFilePathInputStream(FilePath fp)
{
this.fp = fp;
this.is = null;
}

private void initStream() throws IOException {
if (this.is == null) {
try {
this.is = fp.read();
} catch (InterruptedException e) {
throw new IOException("Operation was interrupted", e);
}
}
}

@Override
public int read(byte[] b) throws IOException {
initStream();

int numRead = this.is.read(b);
if (numRead < b.length) {
this.is.close();
}

return numRead;
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
initStream();

int numRead = this.is.read(b, off, len);
if (numRead < len) {
this.is.close();
}

return numRead;
}

@Override
public int read() throws IOException {
initStream();

int result = this.is.read();
if (result < 0) {
this.is.close();
}

return result;
}
}

0 comments on commit b2a790a

Please sign in to comment.