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

[BUG] .value() on empty Optional #4121

Open
dlasusa opened this issue Mar 7, 2025 · 5 comments
Open

[BUG] .value() on empty Optional #4121

dlasusa opened this issue Mar 7, 2025 · 5 comments
Labels
bug Something isn't working mojo Issues that are related to mojo

Comments

@dlasusa
Copy link

dlasusa commented Mar 7, 2025

Bug description

Actual behavior

Mojo crashed and asked I submit a bug report

Expected behavior

Not sure. I'm mostly playing/testing with Mojo. Expected it would print "None" or perhaps unable to print None? Or similar?

Steps to reproduce

from collections import Optional

@value
struct NetworkInfo:
    var target: String
    var prot: String
    var opt: String
    var source: String
    var dest: String
    var service: Optional[String]

    
    fn get_service(self) -> Optional[String]:
        if self.service:
            _full_service_entry = self.service.value()
            try:
                service_list = _full_service_entry.split(' ')
                for item in service_list:
                    if item[].find('dpt:') != -1:
                        _dpt = item[].split(':')
                        if len(_dpt) >= 2:
                            return _dpt[1]
                        else:
                            return String('Unexpected _dpt len: ', len(_dpt))
                return None
            except:
                return None
        else:
            return None

def main():
    # var x = NetworkInfo('my_target', 'my_prot', 'my_opt', 'my_source', 'my_dest', String('tcp dpt:http flags:FIN,SYN,RST,ACK/SYN')) <- this works
    var x = NetworkInfo('my_target', 'my_prot', 'my_opt', 'my_source', 'my_dest', String('tcp'))
    var the_service = x.get_service()
    print(the_service.value())

Crash report

.value() on empty Optional
Please suogram arguments: /home/me/.vscode-server/data/User/globalStorage/modular-mojotools.vscode-mojo/magic-data-home/envs/max/bin/mojo run /home/me/mojo_sys_inventory/main.mojo
Stack dump withoubmit a bug report to https://github.com/modular/mojo/issues and include the crash backtrace along with all the relevant source codes.
Stack dump:
0.      Prt symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  mojo      0x00005572bba4ac0b
1  mojo      0x00005572bba48a79
2  mojo      0x00005572bba4b27d
3  libc.so.6 0x00007fc90f309330
4  libc.so.6 0x00007fc8780036c0
mojo crashed!
Please file a bug report.
Illegal instruction (core dumped)

System information

Magic info:

     Magic version: 0.7.0
System
------------
       Pixi version: 0.41.3
           Platform: linux-64
   Virtual packages: __unix=0=0
                   : __linux=5.15.167.4=0
                   : __glibc=2.39=0
                   : __cuda=12.7=0
                   : __archspec=1=skylake
          Cache dir: /home/me/.cache/rattler/cache
       Auth storage: /home/me/.rattler/credentials.json
   Config locations: No config files found

Global
------------
            Bin dir: /home/me/.modular/bin
    Environment dir: /home/me/.modular/envs
       Manifest dir: /home/me/.modular/manifests/pixi-global.toml

Project
------------
               Name: mojo_sys_inventory
            Version: 0.1.0
      Manifest file: /home/me/mojo_sys_inventory/mojoproject.toml
       Last updated: 06-03-2025 20:16:00

Environments
------------
        Environment: default
           Features: default
           Channels: https://conda.modular.com/max-nightly, https://conda.modular.com/max, https://repo.prefix.dev/modular-community, conda-forge
   Dependency count: 1
       Dependencies: max
   Target platforms: linux-64

magic list max:

Package     Version               Build    Size       Kind   Source
max         25.2.0.dev2025030619  release  9.7 KiB    conda  max
max-core    25.2.0.dev2025030619  release  244.4 MiB  conda  max-core
max-python  25.2.0.dev2025030619  release  121.3 MiB  conda  max-python
@dlasusa dlasusa added bug Something isn't working mojo Issues that are related to mojo labels Mar 7, 2025
@soraros
Copy link
Contributor

soraros commented Mar 7, 2025

I think this is the expected behaviour since it should abort on an empty Optional:

if not self.__bool__():
abort(".value() on empty Optional")

It's more of an issue with using abort for error handling. But I suppose we also couldn't do much better before pattern matching.

Copy link
Collaborator

This could definitely use a better error message behavior.

@dlasusa
Copy link
Author

dlasusa commented Mar 7, 2025

Yeah, I assume the issue is that I should be checking the type/contents of the_service before calling .value() on it? I mostly reported as a bug because the error output asked me to. Happy to close this if the output is expected and appropriate, also happy to leave it open as a reminder for better messaging if that helps?

@ematejska
Copy link
Collaborator

Thank you, we appreciate your report. I passed it along to the engineering team for more feedback.

@illiasheshyn
Copy link

illiasheshyn commented Mar 9, 2025

Just a heads up, there is an existing issue regarding this #3977, however it does not provide as much information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mojo Issues that are related to mojo
Projects
None yet
Development

No branches or pull requests

4 participants