@@ -86,7 +86,6 @@ public Client(
8686 this .baseUrl = (baseUrl != null ? baseUrl : Config .api .baseUrl ).replaceAll ("/$" , "" );
8787
8888 double connTimeout = connectionTimeout != null ? connectionTimeout : Config .api .connectionTimeout ;
89- double totalTimeout = Config .api .timeout ;
9089
9190 // Build HTTP client. OkHttp's retryOnConnectionFailure defaults to true and
9291 // can silently re-send a request (including the submission POST) after a
@@ -95,7 +94,7 @@ public Client(
9594 this .httpClient = new OkHttpClient .Builder ()
9695 .retryOnConnectionFailure (false )
9796 .connectTimeout ((long ) (connTimeout * 1000 ), TimeUnit .MILLISECONDS )
98- .readTimeout ((long ) (totalTimeout * 1000 ), TimeUnit .MILLISECONDS )
97+ .readTimeout ((long ) (Config . api . readTimeout * 1000 ), TimeUnit .MILLISECONDS )
9998 .build ();
10099
101100 this .gson = new Gson ();
@@ -254,15 +253,9 @@ private SubmitResult submit(
254253 body .put ("enable_sync_mode" , true );
255254 }
256255
257- double requestTimeout = timeout != null ? timeout : Config .api .timeout ;
258- double connectTimeout = Math .min (
259- this .httpClient .connectTimeoutMillis () / 1000.0 ,
260- requestTimeout
261- );
262-
263- // Per-call timeouts: connect uses the smaller of the client connect
264- // timeout and the request timeout; read/call enforce the request timeout.
265- OkHttpClient callClient = perCallClient (connectTimeout , requestTimeout );
256+ // Sync mode keeps this POST open until the task finishes, so it only has
257+ // a timeout when one is configured; async submit falls back to readTimeout.
258+ OkHttpClient callClient = requestClient (timeout , !enableSyncMode );
266259
267260 Request request = addClientHeaders (new Request .Builder ()
268261 .url (url )
@@ -329,6 +322,40 @@ private OkHttpClient perCallClient(double connectTimeout, double requestTimeout)
329322 .build ();
330323 }
331324
325+ /**
326+ * Effective configured timeout: the explicit argument, else
327+ * Config.api.timeout (WAVESPEED_TIMEOUT). Null means no limit.
328+ */
329+ static Double configuredTimeout (Double explicit ) {
330+ return explicit != null ? explicit : Config .api .timeout ;
331+ }
332+
333+ /**
334+ * Per-request client. A configured timeout applies as connect (capped),
335+ * read and call timeout, as before. Without one, requests that are bounded
336+ * by default use Config.api.readTimeout for read and call; the others
337+ * (sync submit) have no read/call timeout (OkHttp 0 = none).
338+ */
339+ OkHttpClient requestClient (Double explicit , boolean boundedByDefault ) {
340+ Double configured = configuredTimeout (explicit );
341+ if (configured != null ) {
342+ double connect = Math .min (httpClient .connectTimeoutMillis () / 1000.0 , configured );
343+ return perCallClient (connect , configured );
344+ }
345+ if (boundedByDefault ) {
346+ double rt = Config .api .readTimeout ;
347+ double connect = Math .min (httpClient .connectTimeoutMillis () / 1000.0 , rt );
348+ return perCallClient (connect , rt );
349+ }
350+ return httpClient .newBuilder ()
351+ .readTimeout (0 , TimeUnit .MILLISECONDS )
352+ .callTimeout (0 , TimeUnit .MILLISECONDS )
353+ .build ();
354+ }
355+
356+ /** Clock for the wait loop's overall cap (replaceable in tests). */
357+ java .util .function .LongSupplier nowMillis = System ::currentTimeMillis ;
358+
332359 private static boolean isRetryableStatus (int code ) {
333360 return code == 429 || code == 500 || code == 502 || code == 503 || code == 504 ;
334361 }
@@ -343,12 +370,7 @@ private static boolean isRetryableStatus(int code) {
343370 */
344371 private Map <String , Object > getResult (String requestId , Double timeout ) {
345372 String url = this .baseUrl + "/api/v3/predictions/" + requestId + "/result" ;
346- double requestTimeout = timeout != null ? timeout : Config .api .timeout ;
347- double connectTimeout = Math .min (
348- this .httpClient .connectTimeoutMillis () / 1000.0 ,
349- requestTimeout
350- );
351- OkHttpClient callClient = perCallClient (connectTimeout , requestTimeout );
373+ OkHttpClient callClient = requestClient (timeout , true );
352374
353375 for (int retry = 0 ; retry <= maxConnectionRetries ; retry ++) {
354376 try {
@@ -414,7 +436,7 @@ private Map<String, Object> getResult(String requestId, Double timeout) {
414436 * Wait for prediction to complete.
415437 *
416438 * @param requestId The prediction request ID
417- * @param timeout Maximum wait time in seconds (null = no timeout )
439+ * @param timeout Maximum wait time in seconds (null = Config.api.timeout, which defaults to no limit )
418440 * @param pollInterval Time between polls in seconds
419441 * @return Map with "outputs" array
420442 * @throws RuntimeException if prediction fails
@@ -425,15 +447,17 @@ private Map<String, Object> wait(
425447 Double timeout ,
426448 double pollInterval
427449 ) {
428- long startTime = System .currentTimeMillis ();
450+ // Overall cap: explicit timeout, else Config.api.timeout; null = wait until terminal status.
451+ Double cap = configuredTimeout (timeout );
452+ long startTime = nowMillis .getAsLong ();
429453
430454 while (true ) {
431455 // Check timeout
432- if (timeout != null ) {
433- double elapsed = (System . currentTimeMillis () - startTime ) / 1000.0 ;
434- if (elapsed >= timeout ) {
456+ if (cap != null ) {
457+ double elapsed = (nowMillis . getAsLong () - startTime ) / 1000.0 ;
458+ if (elapsed >= cap ) {
435459 throw new RuntimeException (
436- "Prediction timed out after " + timeout + " seconds (task_id: " + requestId + ")"
460+ "Prediction timed out after " + cap + " seconds (task_id: " + requestId + ")"
437461 );
438462 }
439463 }
@@ -544,7 +568,7 @@ private String syncModeErrorMessage(Map<String, Object> data) {
544568 *
545569 * @param model Model identifier (e.g., "wavespeed-ai/z-image/turbo")
546570 * @param input Input parameters for the model
547- * @param timeout Maximum time to wait for completion (null = no timeout )
571+ * @param timeout Maximum time to wait for completion (null = Config.api.timeout; default no limit )
548572 * @param pollInterval Interval between status checks in seconds (null = 1.0)
549573 * @param enableSyncMode If true, use synchronous mode (best-effort single request) (null = false)
550574 * @param maxRetries Maximum task-level retries (null = use client setting)
@@ -657,7 +681,7 @@ public Map<String, Object> run(String model, Map<String, Object> input, double t
657681 * Upload a file to WaveSpeed.
658682 *
659683 * @param file File path string to upload
660- * @param timeout Total API call timeout in seconds (null = use default)
684+ * @param timeout Request timeout in seconds (null = Config.api.timeout; default no call limit )
661685 * @return URL of the uploaded file
662686 * @throws IllegalArgumentException if API key is not configured
663687 * @throws IllegalArgumentException if file path does not exist
@@ -689,12 +713,8 @@ public String upload(String file, Double timeout) {
689713 payload .put ("content_type" , contentType );
690714 }
691715
692- double requestTimeout = timeout != null ? timeout : Config .api .timeout ;
693- double connectTimeout = Math .min (
694- this .httpClient .connectTimeoutMillis () / 1000.0 ,
695- requestTimeout
696- );
697- OkHttpClient callClient = perCallClient (connectTimeout , requestTimeout );
716+ // Uploads can legitimately take long: only a configured timeout bounds the call.
717+ OkHttpClient callClient = requestClient (timeout , false );
698718
699719 Request request = addClientHeaders (new Request .Builder ()
700720 .url (this .baseUrl + "/api/v3/media/uploads" )
@@ -858,7 +878,7 @@ public RunNoThrowResult(Object outputs, RunDetail detail) {
858878 *
859879 * @param model Model identifier
860880 * @param input Input parameters
861- * @param timeout Maximum time to wait for completion (null = no timeout )
881+ * @param timeout Maximum time to wait for completion (null = Config.api.timeout; default no limit )
862882 * @param pollInterval Interval between status checks in seconds (null = 1.0)
863883 * @param enableSyncMode If true, use synchronous mode (best-effort single request) (null = false)
864884 * @param maxRetries Maximum task-level retries (null = use client setting)
0 commit comments