Skip to content

Commit 70b3380

Browse files
author
AdrianTodt
authored
Merge pull request #31 from rethinkdb/fix/handshake-hang
Rewrite readCString to avoid possible handshake hanging
2 parents 334fdc9 + 157f822 commit 70b3380

File tree

2 files changed

+41
-29
lines changed

2 files changed

+41
-29
lines changed

src/main/java/com/rethinkdb/net/Connection.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,12 @@ public boolean isOpen() {
183183
@Nullable TypeReference<T> typeRef) {
184184
try {
185185
return runAsync(term, optArgs, fetchMode, typeRef).join();
186-
} catch (CompletionException e) {
187-
if (e.getCause() instanceof ReqlError) {
188-
throw ((ReqlError) e.getCause());
186+
} catch (CompletionException ce) {
187+
Throwable t = ce.getCause();
188+
if (t instanceof ReqlError) {
189+
throw ((ReqlError) t);
189190
}
190-
throw e;
191+
throw new ReqlDriverError(t);
191192
}
192193
}
193194

@@ -213,12 +214,12 @@ public boolean isOpen() {
213214
public @NotNull Server server() {
214215
try {
215216
return serverAsync().join();
216-
} catch (
217-
CompletionException e) {
218-
if (e.getCause() instanceof ReqlError) {
219-
throw ((ReqlError) e.getCause());
217+
} catch (CompletionException ce) {
218+
Throwable t = ce.getCause();
219+
if (t instanceof ReqlError) {
220+
throw ((ReqlError) t);
220221
}
221-
throw e;
222+
throw new ReqlDriverError(t);
222223
}
223224
}
224225

@@ -237,11 +238,12 @@ public boolean isOpen() {
237238
public void noreplyWait() {
238239
try {
239240
noreplyWaitAsync().join();
240-
} catch (CompletionException e) {
241-
if (e.getCause() instanceof ReqlError) {
242-
throw ((ReqlError) e.getCause());
241+
} catch (CompletionException ce) {
242+
Throwable t = ce.getCause();
243+
if (t instanceof ReqlError) {
244+
throw ((ReqlError) t);
243245
}
244-
throw e;
246+
throw new ReqlDriverError(t);
245247
}
246248
}
247249

src/main/java/com/rethinkdb/net/DefaultConnectionFactory.java

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,25 +106,30 @@ public void write(@NotNull ByteBuffer buffer) {
106106

107107
@Override
108108
public @NotNull String readCString(@Nullable Long deadline) {
109-
try {
110-
final StringBuilder sb = new StringBuilder();
111-
int next;
112-
char c;
113-
while ((next = inputStream.read()) != -1 && (c = (char) next) != '\0') {
114-
// is there a deadline?
115-
if (deadline != null) {
116-
// have we timed-out?
117-
if (deadline < System.currentTimeMillis()) { // reached time-out
118-
throw new ReqlDriverError("Connection timed out.");
119-
}
109+
Long timeout = deadline == null ? null : System.currentTimeMillis() + deadline;
110+
final StringBuilder b = new StringBuilder();
111+
int has;
112+
int next;
113+
char c;
114+
while (timeout == null || System.currentTimeMillis() < timeout) {
115+
try {
116+
has = inputStream.available();
117+
if (has < 0) {
118+
break;
119+
}
120+
if (has == 0) {
121+
Thread.yield();
122+
continue;
120123
}
121-
sb.append(c);
124+
if ((next = inputStream.read()) == -1 || (c = (char) next) == '\0') {
125+
return b.toString();
126+
}
127+
} catch (IOException e) {
128+
throw new ReqlDriverError(e);
122129
}
123-
124-
return sb.toString();
125-
} catch (IOException e) {
126-
throw new ReqlDriverError(e);
130+
b.append(c);
127131
}
132+
throw new ReqlDriverError("Read timed out.");
128133
}
129134

130135
@Override
@@ -231,5 +236,10 @@ private void shutdown(Exception e) {
231236
public void shutdownPump() {
232237
shutdown(new ReqlDriverError("Response pump closed."));
233238
}
239+
240+
@Override
241+
public String toString() {
242+
return "ThreadResponsePump";
243+
}
234244
}
235245
}

0 commit comments

Comments
 (0)