Skip to content

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

Closed
@schreibfaul1

Description

@schreibfaul1

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.

Activity

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[/+] on Apr 30, 2025
schreibfaul1

schreibfaul1 commented on May 4, 2025

@schreibfaul1
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

kaloprojects commented on May 10, 2025

@kaloprojects

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

me-no-dev

me-no-dev commented on May 13, 2025

@me-no-dev
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

me-no-dev commented on May 13, 2025

@me-no-dev
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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      WiFiClientSecure, Object destroys itself even after the server closes the connection · Issue #11325 · espressif/arduino-esp32