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

Cannot open console input buffer for writing randomly appears during the ahk works #226

Closed
MIFIKUS opened this issue Aug 14, 2023 Discussed in #225 · 8 comments
Closed

Comments

@MIFIKUS
Copy link

MIFIKUS commented Aug 14, 2023

Discussed in #225

Originally posted by MIFIKUS August 14, 2023
Hello!
I made an ahk script for Lineage 2M. And during the script works i get Cannot open console input buffer for writing.
It apperars absolutely randomly.
My code looks like this.

if action == 'move':
            move = f'''\
            #MaxThreads 20
            CoordMode, Mouse, Screen  ;
            DllCall("SetCursorPos", int, {x}, int, {y})
            Sleep, 120
            Exit

            '''
            while autohotkey.get_mouse_position(coord_mode='Screen') != (x, y):
                autohotkey.run_script(move)
            time.sleep(0.9*MULTIPLIER)
            return

        elif action == 'click':
            click = '''\
            #MaxThreads 20
            SendEvent {Click}
            Sleep, 120
            Exit
            '''

            autohotkey.run_script(click)
            time.sleep(0.9*MULTIPLIER)
            return

        elif action == 'drag':
            drag = f'''\
            #MaxThreads 20
            Click down
            Sleep, 120
            MouseMove, {x}, {y}, 1, {direction}
            Sleep, 120
            Click up
            Sleep, 120
            Sleep, 120
            Exit
            '''
            autohotkey.run_script(drag)
            time.sleep(0.5*MULTIPLIER)
            return

        elif action == 'wheel':
            wheel = f'''\
            #MaxThreads 20
            MouseClick,WheelDown,,,1,0,D,R
            Sleep, 120 
            Exit
            '''

I tried to google this error but didn't find anything.
To set a MaxThreads and Exit from script in the end didn't help too

@spyoungtech
Copy link
Owner

Can you provide the full stack trace you get in the error?

@MIFIKUS
Copy link
Author

MIFIKUS commented Aug 16, 2023

Can you provide the full stack trace you get in the error?

Yeah. Sure

Цена выставлена
152

Нашел по прошлым кордам
(224, 114)
152

55

Сообщение в тг отправлено
Process Process-1:

Traceback (most recent call last):
  File "C:\Python\lib\multiprocessing\process.py", line 314, in _bootstrap
    self.run()
  File "C:\Python\lib\multiprocessing\process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "C:\BigBot (18)\BigBot\Autosell\AutoSellBotFinal.py", line 749, in run
    windows.switch_windows(main, current_time)
  File "C:\BigBot (18)\BigBot\Autosell\AutoSellBotFinal.py", line 320, in switch_windows
    func(window, current_time)
  File "C:\BigBot (18)\BigBot\Autosell\AutoSellBotFinal.py", line 727, in main
    ingame.make_new_price(price)
  File "C:\BigBot (18)\BigBot\Autosell\AutoSellBotFinal.py", line 647, in make_new_price
    ahk.mouse_actions('move', x=1055, y=835)
  File "C:\BigBot (18)\BigBot\Autosell\AutoSellBotFinal.py", line 63, in mouse_actions
    autohotkey.run_script(move)
  File "C:\Python\lib\site-packages\ahk\_sync\engine.py", line 1052, in run_script
    return self._transport.run_script(script_text_or_path, blocking=blocking, timeout=timeout)
  File "C:\Python\lib\site-packages\ahk\_sync\transport.py", line 821, in run_script
    proc.start()
  File "C:\Python\lib\site-packages\ahk\_sync\transport.py", line 226, in start
    self._proc = sync_create_process(self.runargs)
  File "C:\Python\lib\site-packages\ahk\_sync\transport.py", line 268, in sync_create_process
    return subprocess.Popen(runargs, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
  File "C:\Python\lib\subprocess.py", line 955, in init
    self.stdin = io.open(p2cwrite, 'wb', bufsize)
ValueError: Cannot open console input buffer for writing

Process finished with exit code -1

@spyoungtech
Copy link
Owner

Hmm. This is very strange. I can't reproduce this issue.

Can you make sure you're using the latest version of ahk and latest version of Python 3.11 and try again?

This may be related to a recently fixed bug in Python with windows fds and short-lived processes.

Another way you may avoid this is by not using run_script. That way, you avoid the call to subprocess.Popen altogether:

            while autohotkey.get_mouse_position(coord_mode='Screen') != (x, y):
-                 autohotkey.run_script(move)
+                 autohotkey.mouse_move(x, y, coord_mode='Screen')

Same goes for most of the other places where run_script is being used. You can probably just replace those with appropriate method calls.

@MIFIKUS
Copy link
Author

MIFIKUS commented Aug 18, 2023

Hmm. This is very strange. I can't reproduce this issue.

Can you make sure you're using the latest version of ahk and latest version of Python 3.11 and try again?

This may be related to a recently fixed bug in Python with windows fds and short-lived processes.

Another way you may avoid this is by not using run_script. That way, you avoid the call to subprocess.Popen altogether:

            while autohotkey.get_mouse_position(coord_mode='Screen') != (x, y):
-                 autohotkey.run_script(move)
+                 autohotkey.mouse_move(x, y, coord_mode='Screen')

Same goes for most of the other places where run_script is being used. You can probably just replace those with appropriate method calls.

I updated python. Use 1.3.0 version of ahk. And now i get this exception

Exception in thread Thread-1209 (_readerthread):
Traceback (most recent call last):
File "C:\Users\MIFIKUS\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 1038, in _bootstrap_inner
self.run()
File "C:\Users\MIFIKUS\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 975, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\MIFIKUS\AppData\Local\Programs\Python\Python311\Lib\subprocess.py", line 1597, in _readerthread
buffer.append(fh.read())
^^^^^^^^^
OSError: [WinError 1] Incorrect function

@MIFIKUS
Copy link
Author

MIFIKUS commented Aug 18, 2023

Also i tried to use ahk.mouse_move() function but for some reason only ahk scripts works with Lineage2M

@spyoungtech
Copy link
Owner

Hmm. There's definitely something wrong here in that last stack trace, but I'm doubtful it actually has anything to do with this library in particular, since no ahk code appears in the stacktrace. The issue you're having seems to come from somewhere else.

I can only speculate as to what the problem may be, but I would double check how you're engaging with threading/multiprocessing to make sure your program is utilizing concurrency in a safe manner. It could also be an environment issue, such as if your machine has network-mounted storage, if you're using an IDE/debugger to run your code, or it could be something else entirely. I'm not sure, really.

Unfortunately, I'm not able to reproduce any of these issues, so I'm not sure how I can help further.

@MIFIKUS
Copy link
Author

MIFIKUS commented Aug 20, 2023

Hmm. There's definitely something wrong here in that last stack trace, but I'm doubtful it actually has anything to do with this library in particular, since no ahk code appears in the stacktrace. The issue you're having seems to come from somewhere else.

I can only speculate as to what the problem may be, but I would double check how you're engaging with threading/multiprocessing to make sure your program is utilizing concurrency in a safe manner. It could also be an environment issue, such as if your machine has network-mounted storage, if you're using an IDE/debugger to run your code, or it could be something else entirely. I'm not sure, really.

Unfortunately, I'm not able to reproduce any of these issues, so I'm not sure how I can help further.

Can this issue appear because i have thread with GUI and another one for script to prevent GIL. And script opens and closes ahk threads. And maybe problem appears because of ahk thread starts from not main thread?

@spyoungtech
Copy link
Owner

Closing this due to inactivity. I also don't think this issue is directly related to any issue with the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants