Skip to content

WiFiClientSecure, Object destroys itself even after the server closes the connection #11325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
schreibfaul1 opened this issue Apr 29, 2025 · 4 comments · May be fixed by #11356
Open
1 task done

WiFiClientSecure, Object destroys itself even after the server closes the connection #11325

schreibfaul1 opened this issue Apr 29, 2025 · 4 comments · May be fixed by #11356
Labels
Status: Awaiting triage Issue is waiting for triage

Comments

@schreibfaul1
Copy link

Board

ESP32 Dev Module

Device Description

ESP32 and ESP32-S3 boards

Hardware Configuration

The board has no external circuit

Version

v3.2.0

IDE Name

Arduino IDE

Operating System

Linux Mint

Flash frequency

80MHz

PSRAM enabled

no

Upload speed

921600

Description

I create an object WiFiClientSecure client, Connect to a server, get the HTTP Response header and the Payoad. As far as everything is expected.
As soon as the server closes the connection, the client no longer exists.

The query client.connected() should return false and the object should continue to exist.
but I get:
fail on 0, errno: 9, "Bad file number" and "client is null"

I wonder if that should be so

Sketch

#include "Arduino.h"
#include "WiFi.h"
#include "WiFiClientSecure.h"

const char *ssid = "*****";
const char *password = "*******";
const char *server = "arduino.tips";  // Server URL
int av;

WiFiClientSecure client;

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }
  log_i("WiFi connected");
  client.setInsecure();
  bool res = client.connect(server, 443);
  if (res) {
    client.print("GET /asciilogo.txt HTTP/1.1\r\nHost: ");
    client.print(server);
    client.print("\r\nConnection: close\r\n\r\n");
  }
}

void loop() {
  vTaskDelay(1);
  if(!client) log_e("client is null");
  if (client.connected()) {
    av = client.available();
    for (int i = 0; i < av; i++) Serial.write(client.read());
  }
}

Debug Message

;;;   ;;;;;`  ;;;;:  .;;  ;; ,;;;;;, ;;. `;,  ;;;;   
    ;;;   ;;:;;;  ;;;;;; .;;  ;; ,;;;;;: ;;; `;, ;;;:;;  
   ,;:;   ;;  ;;  ;;  ;; .;;  ;;   ,;,   ;;;,`;, ;;  ;;  
   ;; ;:  ;;  ;;  ;;  ;; .;;  ;;   ,;,   ;;;;`;, ;;  ;;. 
   ;: ;;  ;;;;;:  ;;  ;; .;;  ;;   ,;,   ;;`;;;, ;;  ;;` 
  ,;;;;;  ;;`;;   ;;  ;; .;;  ;;   ,;,   ;; ;;;, ;;  ;;  
  ;;  ,;, ;; .;;  ;;;;;:  ;;;;;: ,;;;;;: ;;  ;;, ;;;;;;  
  ;;   ;; ;;  ;;` ;;;;.   `;;;:  ,;;;;;, ;;  ;;,  ;;;;   
[  3648][E][sketch_apr29a.ino:31] loop(): client is null
[  3654][E][NetworkClient.cpp:327] setSocketOption(): fail on 0, errno: 9, "Bad file number"
[  3664][E][NetworkClient.cpp:327] setSocketOption(): fail on 0, errno: 9, "Bad file number"

Other Steps to Reproduce

The sketch is enough to reproduce

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@schreibfaul1 schreibfaul1 added the Status: Awaiting triage Issue is waiting for triage label Apr 29, 2025
@schreibfaul1 schreibfaul1 changed the title WiFiClientSecure, Object destroys itself even after the server separates the connection WiFiClientSecure, Object destroys itself even after the server closes the connection Apr 30, 2025
@schreibfaul1
Copy link
Author

There are hosts, such as "api.openai.com", which do not deliver a contentLength after the speech synthesis "/v1/audio/speech".
The audio data is complete when the server closes the connection.

My idea is to use client.connected() to recognise when the payload is complete. But this returns setSocketOption(): fail on 0, errno: 9, "Bad file number" in case the TCP connection was closed. This should be easy reproducible with the sketch.

@kaloprojects
Copy link

@me-no-dev or @P-R-O-C-H-Y : any thoughts a/o plans ?
Thx!

@me-no-dev
Copy link
Member

if(!client) log_e("client is null");

Is the same thing as

if(!client.connected()) log_e("client is not connected");

Client is disconnected and it's underlying socket is gone. The object still exists

@me-no-dev
Copy link
Member

This PR should fix the constant errors. Example above print only client is null in a loop. Again, client is not null, only it's internal socket object is gone. Client can be re-used

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Awaiting triage Issue is waiting for triage
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants