Skip to content

Track cmDNS-default state to restore Device-Name-based hostname#5565

Closed
agners wants to merge 1 commit intowled:mainfrom
agners:fix-hostname-cmdns-default-flag
Closed

Track cmDNS-default state to restore Device-Name-based hostname#5565
agners wants to merge 1 commit intowled:mainfrom
agners:fix-hostname-cmdns-default-flag

Conversation

@agners
Copy link
Copy Markdown

@agners agners commented May 4, 2026

PR #5424 made getWLEDhostname() prefer cmDNS over the legacy serverDescription-based hostname, but cmDNS is still overwritten at boot with "wled-" when it holds the "x" default sentinel. The new hostname check (strcmp_P against DEFAULT_MDNS_NAME) sees the auto-filled value and treats it as user-configured, so users who never customized the mDNS field — and previously got wled-<DeviceName> as their network hostname — now get wled-<mac6> instead.

Track whether the auto-fill was applied via a new cmDNSisDefault flag, and use that in getWLEDhostname(). The flag is set at boot from the comparison against DEFAULT_MDNS_NAME and cleared when the user saves the WiFi settings form. All other consumers (MDNS.begin, ArduinoOTA.setHostname, captive portal, MQTT, bus_manager, improv) keep using the auto-filled cmDNS unchanged.

Fixes: #5563

Summary by CodeRabbit

Improved internal tracking of mDNS hostname configuration to better distinguish between auto-generated and user-configured values, enabling more accurate settings management going forward.

  • Refactor
    • Enhanced hostname configuration state tracking for improved settings management

PR wled#5424 made getWLEDhostname() prefer cmDNS over the legacy
serverDescription-based hostname, but cmDNS is still overwritten at boot
with "wled-<mac6>" when it holds the "x" default sentinel. The new
hostname check (strcmp_P against DEFAULT_MDNS_NAME) sees the auto-filled
value and treats it as user-configured, so users who never customized
the mDNS field — and previously got "wled-<DeviceName>" as their network
hostname — now get "wled-<mac6>" instead.

Track whether the auto-fill was applied via a new cmDNSisDefault flag,
and use that in getWLEDhostname() instead of inspecting cmDNS contents.
The flag is set at boot from the comparison against DEFAULT_MDNS_NAME
and cleared when the user saves the WiFi settings form. All other
consumers (MDNS.begin, ArduinoOTA.setHostname, captive portal, MQTT,
bus_manager, improv) keep using the auto-filled cmDNS unchanged.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 4, 2026

Walkthrough

A new global boolean flag cmDNSisDefault tracks whether the mDNS hostname (cmDNS) is still set to its auto-filled default value or has been explicitly configured by the user. The flag is initialized during setup, updated when the user saves settings, and used to determine hostname preference instead of string comparison.

Changes

mDNS Default-State Tracking

Layer / File(s) Summary
Data Shape
wled00/wled.h
New global cmDNSisDefault flag initialized to true to track whether cmDNS holds the default auto-filled value.
Setup Initialization
wled00/wled.cpp
WLED::setup() sets cmDNSisDefault based on whether cmDNS matches DEFAULT_MDNS_NAME, then conditionally applies unique mDNS autofill only when the flag is true.
Settings Handler
wled00/set.cpp
WiFi settings handler in handleSettingsSet sets cmDNSisDefault = false after user saves an explicit cmDNS value.
Hostname Selection
wled00/util.cpp
getWLEDhostname() uses !cmDNSisDefault flag to determine preferred hostname instead of string comparison against DEFAULT_MDNS_NAME.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Suggested labels

enhancement

Suggested reviewers

  • netmindz
  • DedeHai
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: introducing a cmDNSisDefault flag to track default state and restore Device-Name-based hostname selection.
Linked Issues check ✅ Passed The code changes directly address issue #5563 by introducing cmDNSisDefault flag to distinguish auto-filled from user-set mDNS values, allowing getWLEDhostname() to correctly fall back to Device-Name-based hostname.
Out of Scope Changes check ✅ Passed All changes are tightly scoped to the cmDNS default-state tracking mechanism and hostname selection logic, with no unrelated modifications to other subsystems.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
wled00/wled.cpp (1)

513-515: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

cmDNSisDefault is still at its global initial value (true) when getWLEDhostname() is called here — regression for custom-mDNS users.

cmDNSisDefault is not computed until lines 537–538, which come after this ESP32 hostname block. At line 514, !cmDNSisDefault is always false (initial value), so getWLEDhostname() unconditionally falls through to prepareHostname() (Device-Name path) regardless of whether the user has explicitly set a custom mDNS name.

Before this PR, the same call used strcmp_P(cmDNS, DEFAULT_MDNS_NAME) and correctly detected custom values. This is a regression: on ESP32 with WiFi.persistent(true), the auto-reconnect triggered by WiFi.mode(WIFI_STA) at line 519 may register the wrong DHCP hostname before initConnection() corrects it.

Both prerequisites for the computation — escapedMac (line 471) and cmDNS loaded from config (line 484) — are available before this block, so lines 535–538 can be moved up.

🐛 Proposed fix — move the flag initialisation before the ESP32 hostname block
+  // fill in unique mdns default; remember whether we did so the hostname
+  // logic can distinguish auto-filled vs. user-set values
+  cmDNSisDefault = (strcmp(cmDNS, DEFAULT_MDNS_NAME) == 0);
+  if (cmDNSisDefault) sprintf_P(cmDNS, PSTR("wled-%*s"), 6, escapedMac.c_str() + 6);
+
   `#ifndef` ESP8266
   WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);
   WiFi.persistent(true); // ...
   char hostname[64] = {'\0'};
   getWLEDhostname(hostname, sizeof(hostname), true);
   WiFi.setHostname(hostname);
   `#else`
   WiFi.persistent(false);
   `#endif`
   ...
-  // fill in unique mdns default; remember whether we did so the hostname
-  // logic can distinguish auto-filled vs. user-set values
-  cmDNSisDefault = (strcmp(cmDNS, DEFAULT_MDNS_NAME) == 0);
-  if (cmDNSisDefault) sprintf_P(cmDNS, PSTR("wled-%*s"), 6, escapedMac.c_str() + 6);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@wled00/wled.cpp` around lines 513 - 515, cmDNSisDefault is still true when
getWLEDhostname(hostname, ...) is called, causing custom mDNS names to be
ignored; move the computation that sets cmDNSisDefault (which relies on
escapedMac and cmDNS already available) to before the ESP32 hostname block so
getWLEDhostname sees the correct flag. Specifically, compute/update
cmDNSisDefault (using cmDNS and DEFAULT_MDNS_NAME comparison) before calling
getWLEDhostname/ WiFi.setHostname, ensuring prepareHostname is only used when
appropriate and preventing WiFi.mode(WIFI_STA) from registering the wrong DHCP
hostname prior to initConnection.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@wled00/wled.cpp`:
- Around line 535-539: The config serialization currently writes the runtime
cmDNS directly (id[F("mdns")] = cmDNS;) which will persist auto-filled values
and break cmDNSisDefault detection on reboot; update the serialization in
cfg.cpp so that when cmDNSisDefault is true it writes DEFAULT_MDNS_NAME (the
sentinel) instead of the generated cmDNS, or alternatively write an explicit
cmDNSisDefault boolean flag into the config alongside mdns so the loader can
restore the flag; reference cmDNS, cmDNSisDefault, DEFAULT_MDNS_NAME and the
mdns serialization site (id[F("mdns")]) when making the change.

---

Outside diff comments:
In `@wled00/wled.cpp`:
- Around line 513-515: cmDNSisDefault is still true when
getWLEDhostname(hostname, ...) is called, causing custom mDNS names to be
ignored; move the computation that sets cmDNSisDefault (which relies on
escapedMac and cmDNS already available) to before the ESP32 hostname block so
getWLEDhostname sees the correct flag. Specifically, compute/update
cmDNSisDefault (using cmDNS and DEFAULT_MDNS_NAME comparison) before calling
getWLEDhostname/ WiFi.setHostname, ensuring prepareHostname is only used when
appropriate and preventing WiFi.mode(WIFI_STA) from registering the wrong DHCP
hostname prior to initConnection.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 94f6f2cc-45ba-4eba-b906-b6df9fc15805

📥 Commits

Reviewing files that changed from the base of the PR and between e321514 and fa8359e.

📒 Files selected for processing (4)
  • wled00/set.cpp
  • wled00/util.cpp
  • wled00/wled.cpp
  • wled00/wled.h

Comment thread wled00/wled.cpp
Comment on lines +535 to 539
// fill in unique mdns default; remember whether we did so the hostname
// logic can distinguish auto-filled vs. user-set values
cmDNSisDefault = (strcmp(cmDNS, DEFAULT_MDNS_NAME) == 0);
if (cmDNSisDefault) sprintf_P(cmDNS, PSTR("wled-%*s"), 6, escapedMac.c_str() + 6);
#ifndef WLED_DISABLE_MQTT
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | 🏗️ Heavy lift

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify how cmDNS is serialised in cfg.cpp (write path)
rg -n "cmDNS\|mdns" wled00/cfg.cpp

Repository: wled/WLED

Length of output: 1863


🏁 Script executed:

#!/bin/bash
# Verify how cmDNS is serialised in cfg.cpp (write path)
rg -n "cmDNS\|mdns" wled00/cfg.cpp

Repository: wled/WLED

Length of output: 35


🏁 Script executed:

#!/bin/bash
# Find files related to config serialization
find . -type f -name "*cfg*" -o -name "*config*" | grep -E "\.(cpp|h|hpp)$" | head -20

Repository: wled/WLED

Length of output: 98


🏁 Script executed:

#!/bin/bash
# Search for cmDNS in cfg.cpp with context
rg -A5 -B5 "cmDNS" wled00/cfg.cpp

Repository: wled/WLED

Length of output: 842


The serialization path in cfg.cpp does not guard against persisting auto-filled cmDNS values, defeating this fix on reboot.

The serialization code at id[F("mdns")] = cmDNS; writes the current cmDNS value directly to config without checking cmDNSisDefault. After the auto-fill on line 538 (cmDNS = "wled-<mac>" with cmDNSisDefault = true), the next serializeConfigToFS() call persists the runtime-generated value to cfg.json. On subsequent boots, loading cmDNS = "wled-<mac>" from config causes strcmp(cmDNS, DEFAULT_MDNS_NAME) to evaluate false, making cmDNSisDefault = false and breaking hostname selection logic.

The fix requires either:

  1. Modifying the serialization in cfg.cpp to persist the sentinel value (DEFAULT_MDNS_NAME) when cmDNSisDefault is true, or
  2. Persisting the cmDNSisDefault flag itself to recover the intended behavior on reboot.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@wled00/wled.cpp` around lines 535 - 539, The config serialization currently
writes the runtime cmDNS directly (id[F("mdns")] = cmDNS;) which will persist
auto-filled values and break cmDNSisDefault detection on reboot; update the
serialization in cfg.cpp so that when cmDNSisDefault is true it writes
DEFAULT_MDNS_NAME (the sentinel) instead of the generated cmDNS, or
alternatively write an explicit cmDNSisDefault boolean flag into the config
alongside mdns so the loader can restore the flag; reference cmDNS,
cmDNSisDefault, DEFAULT_MDNS_NAME and the mdns serialization site
(id[F("mdns")]) when making the change.

@softhack007
Copy link
Copy Markdown
Member

softhack007 commented May 4, 2026

@agners before we look further into your PR, we need to clarify if what you experienced is actually a bug, or intended (new) behaviour.

Please see my comments in #5563 and check the description in #5424 for expected behaviour.

@softhack007 softhack007 marked this pull request as draft May 4, 2026 18:05
@softhack007
Copy link
Copy Markdown
Member

Closing - this PR tries to improve backwards compatibility; however it also adds new problems:

  • errors in the persistence path (see comments by the rabbit)
  • failure to detect DEFAULT_MDNS_NAME on fresh installs
  • still leads to different names used by mDNS vs. DHCP/DNS.

While the backwards compatibility problem should receive further investigation, I think this PR does not provide a future-proof solution, sorry.

@softhack007 softhack007 closed this May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DHCP request supplied hostname changed from 0.15.4 -> 16.0.0

2 participants