# Event handling within script modules

**URL:** <https://community.zeek.org/t/event-handling-within-script-modules/5490>\
**Category:** Development\
**Tags:** development\
**Created:** [October 11, 2018, 7:46am UTC](https://community.zeek.org/t/event-handling-within-script-modules/5490 "2018-10-11T07:46:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steffen\_Haas](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Steffen\_Haas](https://community.zeek.org/u/Steffen_Haas)\
**Post date:** [October 11, 2018, 7:46am UTC](https://community.zeek.org/t/event-handling-within-script-modules/5490/1 "2018-10-11T07:46:22Z")

</div>

Not sure if this is a bug or I am using it wrong. Consider the following  
snippet. I start it using `sudo ./bin/bro -i eth0 module_test`.

\*File: share/bro/site/module\_test/raise.bro\*

```auto
module module_test;
# Remove the module to make the example work
# OR define the event handler within another module

export {
        global my_event: event(a: int, s: string);
}

event raise_my_event() {
        local a: int = 42;
        local s: string = "Hello World";
        print(fmt("Raising event with a=%d and s=%s", a, s));
        event my_event(a, s);
}

#module module2;

event my_event(a: int, s: string) {
        print(fmt("Received event with a=%d and s=%s", a, s));
}

event bro_init() {
        local w: interval = 10sec;
        print(fmt("Starting example, waiting for %s", w));
        #"raise_my_event()" is executed immediately
        schedule w {raise_my_event()};
}

```

# Problem 1: Event Handling within module  
The handler for "my\_event" is not invoked as long as it is defined  
within the same module "module\_test".  
- Workaround a): Do not use modules at all  
- Workaround b): Define the handler in a different module  
But why should not it be allowed to raise and handle events within the  
same module?

# Problem 2: Direct execution of scheduled events  
At least within this example, I expected the "raise\_my\_event" to be  
invoked after 10 seconds. However, it is done immediately. Am I using  
the schedule wrong?

I am running the current master (0f550806252b46d3e13be24cd2ab4bb8a63bf49b).

Any help appreciated. Best,  
Steffen.

---

<div class="post-metadata">

**Author:** ![Jon\_Siwek](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@Jon\_Siwek](https://community.zeek.org/u/Jon_Siwek)\
**Post date:** [October 11, 2018, 2:40pm UTC](https://community.zeek.org/t/event-handling-within-script-modules/5490/2 "2018-10-11T14:40:41Z")

</div>

> # Problem 1: Event Handling within module  
> The handler for "my\_event" is not invoked as long as it is defined  
> within the same module "module\_test".  
> - Workaround a): Do not use modules at all  
> - Workaround b): Define the handler in a different module  
> But why should not it be allowed to raise and handle events within the  
> same module?

This is related to issues discussed at [1]. You can get the behavior  
you want, but the short explanation/recommendation is to always use  
the module prefix when raising/handling the event. So, in your  
example, when you raise and handle the event, explicitly add  
"module\_test::" even when inside the module.

> # Problem 2: Direct execution of scheduled events  
> At least within this example, I expected the "raise\_my\_event" to be  
> invoked after 10 seconds. However, it is done immediately. Am I using  
> the schedule wrong?

When there's no input sources (not listening on an interface) and  
you're just running a script, timers may get processed as fast as  
possible, but you can add this and you'll then see timers behave based  
on wall clock:

&nbsp;&nbsp;&nbsp;&nbsp;redef exit\_only\_after\_terminate = T;

- Jon

[1] [https://github.com/bro/bro/issues/163](https://github.com/bro/bro/issues/163)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/zeek/original/1X/f09d732bc2cc7c7cc7e35db67cf4e1d5233ce7a7.png) [@system](https://community.zeek.org/u/system)\
**Post date:** [May 6, 2022, 3:46pm UTC](https://community.zeek.org/t/event-handling-within-script-modules/5490/3 "2022-05-06T15:46:07Z")

</div>

Closed on May 6, 2022, 3:46pm UTC


