Skip to content

raspberrypi Out of MDNS service slots #9127

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

Closed
anecdata opened this issue Apr 1, 2024 · 3 comments
Closed

raspberrypi Out of MDNS service slots #9127

anecdata opened this issue Apr 1, 2024 · 3 comments
Labels
bug network rp2040 Raspberry Pi RP2040
Milestone

Comments

@anecdata
Copy link
Member

anecdata commented Apr 1, 2024

CircuitPython version

Adafruit CircuitPython 9.1.0-beta.0 on 2024-03-28; Adafruit QT Py ESP32-S3 4MB Flash 2MB PSRAM with ESP32S3

vs.

Adafruit CircuitPython 9.1.0-beta.0 on 2024-03-28; Raspberry Pi Pico W with rp2040

Code/REPL

import time
import os
import traceback
import microcontroller
import wifi
import socketpool
import mdns

PORT1 = 8086
PORT2 = 8088

time.sleep(3)  # wait for serial
print(f'{os.uname()=}')
print(f'{microcontroller.cpu.reset_reason=}')  # is device just coming off a reset?
print(f'{os.getenv("CIRCUITPY_WEB_API_PASSWORD")=}')  # check for web workflow

mdns_server = mdns.Server(wifi.radio)
mdns_server.hostname = "slots-code-py"
try:
    print(f"http://{mdns_server.hostname}.local.:{PORT1} starting...")
    mdns_server.advertise_service(service_type="_fake", protocol="_udp", port=PORT1)
    print(f"http://{mdns_server.hostname}.local.:{PORT2} starting...")
    mdns_server.advertise_service(service_type="_blah", protocol="_tcp", port=PORT2)
except RuntimeError as ex:
    traceback.print_exception(ex, ex, ex.__traceback__)
print("Done.")

Behavior

Fresh reset in each case. Web workflow is not enabled.

espressif allows (as expected) multiple unique service-protocol-port combinations. raspberrypi does not.

The only limitation noted in the docs is:

service_type and protocol can only occur on one port. Any call after the first will update the entry’s port.

https://docs.circuitpython.org/en/latest/shared-bindings/mdns/index.html#mdns.Server.advertise_service

espressif output (successful):

os.uname()=(sysname='ESP32S3', nodename='ESP32S3', release='9.1.0', version='9.1.0-beta.0 on 2024-03-28', machine='Adafruit QT Py ESP32-S3 4MB Flash 2MB PSRAM with ESP32S3')
microcontroller.cpu.reset_reason=microcontroller.ResetReason.SOFTWARE
os.getenv("CIRCUITPY_WEB_API_PASSWORD")=None
http://slots-code-py.local.:8086 starting...
http://slots-code-py.local.:8088 starting...
Done.

raspberrypi output (unsuccessful):

os.uname()=(sysname='rp2040', nodename='rp2040', release='9.1.0', version='9.1.0-beta.0 on 2024-03-28', machine='Raspberry Pi Pico W with rp2040')
microcontroller.cpu.reset_reason=microcontroller.ResetReason.SOFTWARE
os.getenv("CIRCUITPY_WEB_API_PASSWORD")=None
http://slots-code-py.local.:8086 starting...
http://slots-code-py.local.:8088 starting...
Traceback (most recent call last):
  File "code.py", line 32, in <module>
RuntimeError: Out of MDNS service slots
Done.

If web workflow is enabled, results are similar...

espressif output (successful):

os.uname()=(sysname='ESP32S3', nodename='ESP32S3', release='9.1.0', version='9.1.0-beta.0 on 2024-03-28', machine='Adafruit QT Py ESP32-S3 4MB Flash 2MB PSRAM with ESP32S3')
microcontroller.cpu.reset_reason=microcontroller.ResetReason.SOFTWARE
os.getenv("CIRCUITPY_WEB_API_PASSWORD")=passw0rd
http://slots-code-py.local.:8086 starting...
http://slots-code-pylocal.:8088 starting...
Done.

raspberrypi output (unsuccessful - fails on the first .advertise_service):

os.uname()=(sysname='rp2040', nodename='rp2040', release='9.1.0', version='9.1.0-beta.0 on 2024-03-28', machine='Raspberry Pi Pico W with rp2040')
microcontroller.cpu.reset_reason=microcontroller.ResetReason.SOFTWARE
os.getenv("CIRCUITPY_WEB_API_PASSWORD")=passw0rd
http://slots-code-py.local.:8086 starting...
Traceback (most recent call last):
  File "code.py", line 20, in <module>
RuntimeError: Out of MDNS service slots
Done.
@anecdata anecdata added bug network rp2040 Raspberry Pi RP2040 labels Apr 1, 2024
@tannewt tannewt added this to the Long term milestone Apr 1, 2024
@eightycc
Copy link
Collaborator

Re-tested with CircuitPython 9.2.5. Issue not resolved.

@eightycc
Copy link
Collaborator

lwip configuration macro MDNS_MAX_SERVICES not specified for raspberrypi port, so it was defaulting to 1. Increased to 25, the value specified in the espressif port. Results:

os.uname()=(sysname='rp2040', nodename='rp2040', release='9.2.5', version='9.2.5-11-ga0b482c280-dirt)
microcontroller.cpu.reset_reason=microcontroller.ResetReason.SOFTWARE
os.getenv("CIRCUITPY_WEB_API_PASSWORD")=jk
http://slots-code-py.local.:8086 starting...
http://slots-code-py.local.:8088 starting...
Done.

@anecdata
Copy link
Member Author

I just wanted to check if there was any memory impact, and it's <1KB for 25 slots. Haven't (yet) tried running services on all of those slots. Thanks, eightycc!

code.py.
# Adafruit CircuitPython 9.2.6 on 2025-03-23; Pimoroni Pico Plus 2 W with rp2350b
import time
import os
import gc
import random
import traceback
import microcontroller
import wifi
import mdns

BASE_PORT = 8000
NUM_SLOTS = 26  # up to 25 supported, RuntimeError: Out of MDNS service slots on the 26th

def random_service():
    svc = bytearray(b'_')
    for _ in range(0, 8):
        svc.append(random.choice(b'abcdefghijknlmnopqrstuvwxyz'))
    return svc.decode()

time.sleep(3)  # wait for serial
print(f'{os.uname()=}')
print(f'{microcontroller.cpu.reset_reason=}')  # is device just coming off a reset?
print(f'{os.getenv("CIRCUITPY_WEB_API_PASSWORD")=}')  # check for web workflow
gc.collect()
print(f'{gc.mem_free()=}')

mdns_server = mdns.Server(wifi.radio)
try:
    for slot in range(NUM_SLOTS):
        service_type = random_service()
        protocol = '_udp'
        port = BASE_PORT + slot
        print(f"Starting {service_type}.{protocol} on {mdns_server.hostname}.local.:{port}")
        mdns_server.advertise_service(service_type=service_type, protocol=protocol, port=port)
        gc.collect()
        print(f'{gc.mem_free()=}')
except RuntimeError as ex:
    traceback.print_exception(ex, ex, ex.__traceback__)
print("Done.")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug network rp2040 Raspberry Pi RP2040
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants