Skip to content
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

mac: Checking for existence of a key in a map using the 'in' keyword causes a segfault #22121

Closed
tomialagbe opened this issue Aug 27, 2024 · 7 comments · Fixed by #22125
Closed
Labels
OS: Mac Bugs/feature requests, that are specific to Mac OS. Status: Confirmed This bug has been confirmed to be valid by a contributor.

Comments

@tomialagbe
Copy link

tomialagbe commented Aug 27, 2024

V doctor:

V full version: V 0.4.7 e560553.1085f45
OS: macos, macOS, 14.5, 23F79
Processor: 10 cpus, 64bit, little endian, Apple M1 Pro

getwd: /Users/tomialagbe/dev-projects
vexe: /Users/tomialagbe/v/v
vexe mtime: 2024-08-27 10:35:08

vroot: OK, value: /Users/tomialagbe/v
VMODULES: OK, value: /Users/tomialagbe/.vmodules
VTMP: OK, value: /tmp/v_501

Git version: git version 2.39.3 (Apple Git-146)
Git vroot status: weekly.2024.34-48-g1085f458
.git/config present: true

CC version: Apple clang version 15.0.0 (clang-1500.3.9.4)
thirdparty/tcc status: thirdparty-macos-arm64 713692d4

What did you do?
./v -g -o vdbg cmd/v && ./vdbg event_bus_trial.v

module main

pub struct EventBus[T] {
mut:
	subscribers map[string][]Subscriber[T]
}

type EventType = string
type Subscriber[T] = fn (T)

pub fn new_event_bus[T]() &EventBus[T] {
	return &EventBus[T]{
		subscribers: map[string][]Subscriber[T]{}
	}
}

fn (shared eb EventBus[T]) subscribe(event_type EventType, subscriber Subscriber[T]) {
	lock eb {
		if event_type !in eb.subscribers {
			eb.subscribers[event_type] = []Subscriber[T]{}
		}
		
		eb.subscribers[event_type] << subscriber
	}
}

fn (shared eb EventBus[T]) publish(event_type EventType, data T) {
	lock eb {
		for _, subscriber in eb.subscribers[event_type] {
			subscriber(data)
		}
	}
}

fn main() {
	mut eb := new_event_bus[string]()
	eb.subscribe('test', fn (data string) {
		println(data)
	})
	eb.publish('test', 'hello')
}

What did you expect to see?

hello

What did you see instead?

signal 11: segmentation fault
0   libsystem_platform.dylib            0x000000018a02b584 _sigtramp + 56
1   event_bus_trial                     0x0000000104fa02cc map_key_to_index + 36
2   event_bus_trial                     0x0000000104f883a4 map_exists + 32
3   event_bus_trial                     0x0000000104fc7dcc main__EventBus_T_string_subscribe_T_string + 112
4   event_bus_trial                     0x0000000104fc8070 main__main + 84
5   event_bus_trial                     0x0000000104fc8760 main + 84
6   dyld                                0x0000000189c720e0 start + 2360

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@JalonSolov
Copy link
Contributor

I cannot reproduce the problem on my Linux machine. How did you trigger it? When I run the program after compiling as shown, it just hangs until I hit Ctrl-C:

[jalon@7950x v]$ ./v -g -o vdbg cmd/v && ./vdbg event_bus_trial.v
[jalon@7950x v]$ ./event_bus_trial
^C
[jalon@7950x v]$

@spytheman
Copy link
Member

It is reproducible on macos:
0[15:17:22]@m1: (master) /opt/v $ v -cc clang run ee.v
signal 11: segmentation fault
0 libsystem_platform.dylib 0x00000001805af584 _sigtramp + 56
1 ee 0x00000001045542cc map_key_to_index + 36
2 ee 0x000000010453c3a4 map_exists + 32
3 ee 0x000000010457bdcc main__EventBus_T_string_subscribe_T_string + 112
4 ee 0x000000010457c070 main__main + 84
5 ee 0x000000010457c760 main + 84
6 dyld 0x00000001801f60e0 start + 2360
139[15:17:34]@m1: (master) /opt/v $

@JalonSolov JalonSolov changed the title Checking for existence of a key in a map using the 'in' keyword causes a segfault mac: Checking for existence of a key in a map using the 'in' keyword causes a segfault Aug 27, 2024
@JalonSolov JalonSolov added Status: Confirmed This bug has been confirmed to be valid by a contributor. OS: Mac Bugs/feature requests, that are specific to Mac OS. labels Aug 27, 2024
@yuyi98
Copy link
Member

yuyi98 commented Aug 28, 2024

mut eb := new_event_bus[string]()
It's wrong to use it here.
We should use shared eb := new_event_bus[string]().

@spytheman
Copy link
Member

mut eb := new_event_bus[string]() It's wrong to use it here. We should use shared eb := new_event_bus[string]().

That is right.
I think we should add a checker error, for mut eb, used for methods with shared receivers.

struct Abc { x int }
fn f(shared a Abc) { lock a { println(a.x) } }
fn (shared a Abc) m(){ lock a { println(a.x) } }
// mut a := Abc{}
// f(a) // checker error

mut b := Abc{} // if b is not mutable, there is a checker error, telling the user to declare `b` with `mut` to make it mutable; the message can be changed too, to mention that it should be `shared` too
b.m() // no checker error, but it is better to have one, similar to the `f(a)` case above

@yuyi98
Copy link
Member

yuyi98 commented Aug 28, 2024

Yes, we should add a checker error.

@medvednikov
Copy link
Member

I've removed shared receivers from V. Passing tests, will be live soon.

@medvednikov
Copy link
Member

(as discussed on discord)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OS: Mac Bugs/feature requests, that are specific to Mac OS. Status: Confirmed This bug has been confirmed to be valid by a contributor.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants