Discoveryinterval not stopping

I have the following Discovery Code:

function Discovery.discovery_Handler(driver, _, should_continue)
	log.info("Starting Discovery-------")

	local foundDevices = {}
	
	--Get known devices
	local knownDevices = {}
	local deviceList = driver:get_devices();
		for _, device in ipairs(deviceList) do
			knownDevices[device.device_network_id] = true
		end
	
	while should_continue() do 
		--Searching WLED mDNS Space
		local dnsSpace = config.MDNS_SERVICE_NAME
			
		local discovery_resp = mDNS.discover("_wled._tcp", "local") or {}
		
		log.info(utils.stringify_table(discovery_resp))
		if discovery_resp.found ~= nil then
			
			for _, answer in ipairs(discovery_resp.found) do
			log.info("Devices found")
					local hostname = answer.host_info.name:match("[^%.]+")
					
					if not knownDevices[hostname] and not foundDevices[hostname] then
				
						local metaData = {
							type = "LAN",
							device_network_id = hostname,
							label = hostname,
							profile = config.MAIN_PROFILE_NAME,
							manufacturer = "Aircookie",
							model = "WLED"
						}
				
						--Add Device
						log.info("Trying to create Device with: "..utils.stringify_table(metaData))
						assert(driver:try_create_device(metaData))
						foundDevices[hostname] = true
			
					else
						log.info("Discovered known Device: "..hostname)
					end
				
			end
		end
	end
	
	log.info("Stopping Discovery-------")
	
end

It works perfectly fine but for some reason it keeps running after leaving the discovery Page in the app. I honestly don’t see the problem. Does anyone have an Idea as to why this is happening?

Im on Firmware Version 46.00010

Best Regards
BumBumGame

I’m rather out of my depth but I seem to remember that the should_continue loop needs to have a brief sleep on each iteration to allow the should_continue to become false.

Unfortunately it didnt fix the Problem. I added socket.sleep as such:

function Discovery.discovery_Handler(driver, _, should_continue)
	log.info("Starting Discovery-------")

	local foundDevices = {}
	
	--Get known devices
	local knownDevices = {}
	local deviceList = driver:get_devices();
		for _, device in ipairs(deviceList) do
			knownDevices[device.device_network_id] = true
		end
	
	while should_continue() do 
		--Searching WLED mDNS Space
		local dnsSpace = config.MDNS_SERVICE_NAME
			
		local discovery_resp = mDNS.discover("_wled._tcp", "local") or {}
		
		log.info(utils.stringify_table(discovery_resp))
		if discovery_resp.found ~= nil then
			
			for _, answer in ipairs(discovery_resp.found) do
			log.info("Devices found")
					local hostname = answer.host_info.name:match("[^%.]+")
					
					if not knownDevices[hostname] and not foundDevices[hostname] then
				
						local metaData = {
							type = "LAN",
							device_network_id = hostname,
							label = hostname,
							profile = config.MAIN_PROFILE_NAME,
							manufacturer = "Aircookie",
							model = "WLED"
						}
				
						--Add Device
						log.info("Trying to create Device with: "..utils.stringify_table(metaData))
						assert(driver:try_create_device(metaData))
						foundDevices[hostname] = true
			
					else
						log.info("Discovered known Device: "..hostname)
					end
				
			end
		end
		
		--small sleep on end of discovery
		socket.sleep(1)
	end
	
	log.info("Stopping Discovery-------")
	
end

It only runs once a second now which means the function is working. But unfortunately it still doesnt stop until the Hub is restarted.

You definitely need the sleep so your loop will yield and give that platform a chance to shut it down. That part of the loop seems fine. I would check to see if it is hanging somewhere else. You trace through the internal logic of the loop? Does it truly keep running and is it hanging?

Here’s what chatGPT says:

The loop in the Discovery.discovery_Handler() function continues as long as the should_continue() function returns true.

Based on the code provided, it’s unclear how the should_continue() function is defined and what conditions cause it to return false. It’s possible that should_continue() always returns true, causing the loop to never end.

If you want to investigate further, you should look for the implementation of the should_continue() function and see if there are any circumstances under which it returns false. You may also want to add additional logging statements to help debug the issue.

I just did some testing and it truly does keep running. I might have to reset my hub. I removed everything from the discovery fucntion except the should_continue() loop with socket.sleep and a log function in it. And even without any functional Code shoud_continue() still returns true. I kept it running for 10 Minutes and it didn’t stop.