@@ -72,6 +72,12 @@ final class ResumableUploadFutureImpl<ResponseT> implements ResumableUploadFutur
7272
7373 private volatile @ Nullable String uploadSessionUrl ;
7474
75+ @ GuardedBy ("lock" )
76+ private boolean done ;
77+
78+ @ GuardedBy ("lock" )
79+ private boolean payloadClosed ;
80+
7581 @ GuardedBy ("lock" )
7682 private @ Nullable ApiFuture <?> inFlightFuture ;
7783
@@ -91,15 +97,11 @@ static <ResponseT> ResumableUploadFutureImpl<ResponseT> create(
9197 ResumableUploadFutureImpl <ResponseT > future =
9298 new ResumableUploadFutureImpl <>(
9399 startFuture , uploadChunkCallable , payload , settings , callContext );
94- try {
95- future .start ();
96- } catch (Throwable t ) {
97- future .fail (t );
98- }
100+ future .start ();
99101 return future ;
100102 }
101103
102- private ResumableUploadFutureImpl (
104+ ResumableUploadFutureImpl (
103105 ApiFuture <ResumableUploadSession > startFuture ,
104106 UnaryCallable <ChunkUploadRequest , ChunkUploadResponse <ResponseT >> uploadChunkCallable ,
105107 InputStream payload ,
@@ -112,84 +114,94 @@ private ResumableUploadFutureImpl(
112114 this .settings = checkNotNull (settings , "settings must not be null" );
113115 checkArgument (settings .getChunkSize () > 0 , "chunkSize must be > 0" );
114116 this .callContext = checkNotNull (callContext , "callContext must not be null" );
115- this .inFlightFuture = startFuture ;
117+ synchronized (lock ) {
118+ this .inFlightFuture = startFuture ;
119+ }
116120 }
117121
118- private void start () {
122+ void start () {
119123 ApiFutures .addCallback (
120124 startFuture ,
121125 new ApiFutureCallback <ResumableUploadSession >() {
122126 @ Override
123127 public void onSuccess (ResumableUploadSession session ) {
124- if (resultFuture .isDone ()) {
125- return ;
126- }
127- uploadSessionUrl = session .getUploadUrl ();
128- ResumableUploadChunkCoordinator <ResponseT > coordinator =
129- new ResumableUploadChunkCoordinator <>(
130- uploadChunkCallable ,
131- session .getUploadUrl (),
132- payload ,
133- settings .getChunkSize (),
134- callContext ,
135- ResumableUploadFutureImpl .this );
136- try {
137- coordinator .start ();
138- } catch (Throwable t ) {
139- fail (t );
128+ ApiFuture <ResponseT > uploadFuture ;
129+ synchronized (lock ) {
130+ if (done ) {
131+ return ;
132+ }
133+ uploadSessionUrl = session .getUploadUrl ();
134+ ResumableUploadChunkCoordinator <ResponseT > coordinator =
135+ new ResumableUploadChunkCoordinator <>(
136+ uploadChunkCallable ,
137+ uploadSessionUrl ,
138+ payload ,
139+ settings .getChunkSize (),
140+ callContext );
141+ uploadFuture = coordinator .start ();
142+ inFlightFuture = uploadFuture ;
140143 }
144+ ApiFutures .addCallback (
145+ uploadFuture ,
146+ new ApiFutureCallback <ResponseT >() {
147+ @ Override
148+ public void onSuccess (ResponseT response ) {
149+ finish (response , null );
150+ }
151+
152+ @ Override
153+ public void onFailure (Throwable t ) {
154+ if (t instanceof CancellationException ) {
155+ return ;
156+ }
157+ finish (null , t );
158+ }
159+ },
160+ MoreExecutors .directExecutor ());
141161 }
142162
143163 @ Override
144164 public void onFailure (Throwable t ) {
145- if (t instanceof CancellationException || resultFuture . isDone () ) {
165+ if (t instanceof CancellationException ) {
146166 return ;
147167 }
148- fail ( t );
168+ finish ( null , t );
149169 }
150170 },
151171 MoreExecutors .directExecutor ());
152172 }
153173
154- /**
155- * Registers the active in-flight future for cancellation. If this session future has already been
156- * canceled, the supplied future is canceled immediately.
157- */
158- void setInFlightFuture (ApiFuture <?> inFlightFuture ) {
159- boolean shouldCancel = false ;
174+ private void finish (@ Nullable ResponseT response , @ Nullable Throwable error ) {
160175 synchronized (lock ) {
161- if (resultFuture .isDone ()) {
162- shouldCancel = resultFuture .isCancelled ();
163- } else {
164- this .inFlightFuture = inFlightFuture ;
176+ if (done ) {
177+ return ;
165178 }
166- }
167- if (shouldCancel ) {
168- inFlightFuture .cancel (true );
169- }
170- }
171-
172- void succeed (@ Nullable ResponseT result ) {
173- synchronized (lock ) {
179+ done = true ;
174180 inFlightFuture = null ;
175181 }
176- closePayload ();
177- resultFuture .set (result );
182+ IOException closeError = closePayload ();
183+ if (error == null ) {
184+ resultFuture .set (response );
185+ } else {
186+ if (closeError != null ) {
187+ error .addSuppressed (closeError );
188+ }
189+ resultFuture .setException (error );
190+ }
178191 }
179192
180- void fail ( Throwable t ) {
193+ private @ Nullable IOException closePayload ( ) {
181194 synchronized (lock ) {
182- inFlightFuture = null ;
195+ if (payloadClosed ) {
196+ return null ;
197+ }
198+ payloadClosed = true ;
183199 }
184- closePayload ();
185- resultFuture .setException (t );
186- }
187-
188- private void closePayload () {
189200 try {
190201 payload .close ();
191- } catch (IOException ignored ) {
192- // Suppressed during stream cleanup
202+ return null ;
203+ } catch (IOException e ) {
204+ return e ;
193205 }
194206 }
195207
@@ -205,18 +217,20 @@ public void addListener(Runnable listener, Executor executor) {
205217
206218 @ Override
207219 public boolean cancel (boolean mayInterruptIfRunning ) {
208- boolean cancelled ;
209220 ApiFuture <?> inFlight ;
210221 synchronized (lock ) {
211- cancelled = resultFuture .cancel (mayInterruptIfRunning );
222+ if (done ) {
223+ return resultFuture .isCancelled ();
224+ }
225+ done = true ;
212226 inFlight = this .inFlightFuture ;
213227 this .inFlightFuture = null ;
214228 }
215229 if (inFlight != null ) {
216230 inFlight .cancel (mayInterruptIfRunning );
217231 }
218232 closePayload ();
219- return cancelled ;
233+ return resultFuture . cancel ( mayInterruptIfRunning ) ;
220234 }
221235
222236 @ Override
0 commit comments