Recovering nested notification behavior

You are reverse engineering an event dispatcher whose subscriber list can change during a notification. The complete Python script is shown below. There are no other subscribers, exceptions, threads, or calls.


Each publish call invokes its subscribers in list order. The expression list(subscribers) creates a separate copy when that invocation enters its loop. Later additions to subscribers do not alter an existing copy. Calls execute synchronously: a caller waits for each called function to return.


Python
subscribers = []
log = []

def publish(message):
    for callback in list(subscribers):
        callback(message)

def metrics(message):
    log.append(("metrics", message))

def audit(message):
    log.append(("audit", message))
    if message == "nested":
        subscribers.append(metrics)

def capture(message):
    log.append(("capture", message))
    if message == "start":
        subscribers.append(audit)
        publish("nested")

subscribers.append(capture)
publish("start")
publish("finish")


Determine three exact integer counts: the total number of callback invocations across capture, audit, and metrics; the number of audit invocations; and the maximum number of simultaneously active publish invocations. A publish invocation remains active from entry until return, including while it waits for a nested call.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Reverse Engineering Questions: