robotgo icon indicating copy to clipboard operation
robotgo copied to clipboard

error in use robotgo.EventEnd()

Open yuzhengjun1204 opened this issue 4 years ago • 5 comments

my code:

package main

import (
	"fmt"
	"time"

	"github.com/go-vgo/robotgo"
)

func main() {
	names := robotgo.GetTitle()

	fmt.Println("names: ", names)

	for {
		EvChan := robotgo.EventStart()
		//defer
		for ev := range EvChan {
			fmt.Println("robotgo loop", ev)
			robotgo.EventEnd()
			time.Sleep(time.Second * 1)

		}
	}

}

in linux: ubuntu20.04

Log:

names:  root@ubuntu: /home/yzj/Desktop
robotgo loop 2021-10-11 20:54:57.36914096 -0700 PDT m=+0.083050435 - Event: {Kind: HookEnabled}
robotgo loop 2021-10-11 20:54:58.431772306 -0700 PDT m=+1.145681766 - Event: {Kind: HookEnabled}
robotgo loop 2021-10-11 20:54:59.496055456 -0700 PDT m=+2.209964915 - Event: {Kind: HookEnabled}
robotgo loop 2021-10-11 20:55:00.559746397 -0700 PDT m=+3.273655858 - Event: {Kind: HookEnabled}
robotgo loop 2021-10-11 20:55:01.623433009 -0700 PDT m=+4.337342468 - Event: {Kind: HookEnabled}
robotgo loop 2021-10-11 20:55:02.688602892 -0700 PDT m=+5.402512351 - Event: {Kind: HookEnabled}
[xcb] Unknown request in queue while dequeuing
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
test: ../../src/xcb_io.c:163: dequeue_pending_request: Assertion `!xcb_xlib_unknown_req_in_deq' failed.
SIGABRT: abort
PC=0x7f49d6e3a18b m=5 sigcode=18446744073709551610

goroutine 0 [idle]:
runtime: unknown pc 0x7f49d6e3a18b
stack: frame={sp:0x7f49ae8c6a20, fp:0x0} stack=[0x7f49ae0c7288,0x7f49ae8c6e88)
00007f49ae8c6920:  0000000000000002  00007f49d6dff088 
00007f49ae8c6930:  00007f49ae8c6cd0  00007f49d7599c2e 
00007f49ae8c6940:  0000000000000022  00007f49d748eb20 
00007f49ae8c6950:  00000000000000a3  00007f49d748e6c3 
00007f49ae8c6960:  00007f49d748e6f5  0000000000000022 
00007f49ae8c6970:  0000000000000000  0000015800000003 
00007f49ae8c6980:  00007f49980008d8  0000000000000050 
00007f49ae8c6990:  0000000000000000  0000ffff00001fa0 
00007f49ae8c69a0:  0000000000000000  0000000000000000 
00007f49ae8c69b0:  00007f4998003c60  00007f4998003cc4 
00007f49ae8c69c0:  0000005b0000006e  00007f4998003cc4 
00007f49ae8c69d0:  33363149ae8c6a30  1598d6e73fca6f00 
00007f49ae8c69e0:  00007f4998003c60  00007f49ae8c6c80 
00007f49ae8c69f0:  0000000000000068  0000000000000068 
00007f49ae8c6a00:  0000000000000069  00007f49980278a0 
00007f49ae8c6a10:  00007f49ae8c6a30  00007f49d6e82de6 
00007f49ae8c6a20: <0000000000000000  00007f49d6e91850 
00007f49ae8c6a30:  00007f49fbad8000  00007f49980278a0 
00007f49ae8c6a40:  00007f4998027905  00007f49980278a0 
00007f49ae8c6a50:  00007f49980278a0  00007f4998027908 
00007f49ae8c6a60:  00007f49980279cc  00007f49980278a0 
00007f49ae8c6a70:  00007f49980279cc  0000000000000000 
00007f49ae8c6a80:  0000000000000000  0000000000000000 
00007f49ae8c6a90:  0000000000000000  0000000000000000 
00007f49ae8c6aa0:  fffffffe7fffffff  ffffffffffffffff 
00007f49ae8c6ab0:  ffffffffffffffff  ffffffffffffffff 
00007f49ae8c6ac0:  ffffffffffffffff  ffffffffffffffff 
00007f49ae8c6ad0:  ffffffffffffffff  ffffffffffffffff 
00007f49ae8c6ae0:  ffffffffffffffff  ffffffffffffffff 
00007f49ae8c6af0:  ffffffffffffffff  ffffffffffffffff 
00007f49ae8c6b00:  ffffffffffffffff  ffffffffffffffff 
00007f49ae8c6b10:  ffffffffffffffff  ffffffffffffffff 
runtime: unknown pc 0x7f49d6e3a18b
stack: frame={sp:0x7f49ae8c6a20, fp:0x0} stack=[0x7f49ae0c7288,0x7f49ae8c6e88)
00007f49ae8c6920:  0000000000000002  00007f49d6dff088 
00007f49ae8c6930:  00007f49ae8c6cd0  00007f49d7599c2e 
00007f49ae8c6940:  0000000000000022  00007f49d748eb20 
00007f49ae8c6950:  00000000000000a3  00007f49d748e6c3 
00007f49ae8c6960:  00007f49d748e6f5  0000000000000022 
00007f49ae8c6970:  0000000000000000  0000015800000003 
00007f49ae8c6980:  00007f49980008d8  0000000000000050 
00007f49ae8c6990:  0000000000000000  0000ffff00001fa0 
00007f49ae8c69a0:  0000000000000000  0000000000000000 
00007f49ae8c69b0:  00007f4998003c60  00007f4998003cc4 
00007f49ae8c69c0:  0000005b0000006e  00007f4998003cc4 
00007f49ae8c69d0:  33363149ae8c6a30  1598d6e73fca6f00 
00007f49ae8c69e0:  00007f4998003c60  00007f49ae8c6c80 
00007f49ae8c69f0:  0000000000000068  0000000000000068 
00007f49ae8c6a00:  0000000000000069  00007f49980278a0 
00007f49ae8c6a10:  00007f49ae8c6a30  00007f49d6e82de6 
00007f49ae8c6a20: <0000000000000000  00007f49d6e91850 
00007f49ae8c6a30:  00007f49fbad8000  00007f49980278a0 
00007f49ae8c6a40:  00007f4998027905  00007f49980278a0 
00007f49ae8c6a50:  00007f49980278a0  00007f4998027908 
00007f49ae8c6a60:  00007f49980279cc  00007f49980278a0 
00007f49ae8c6a70:  00007f49980279cc  0000000000000000 
00007f49ae8c6a80:  0000000000000000  0000000000000000 
00007f49ae8c6a90:  0000000000000000  0000000000000000 
00007f49ae8c6aa0:  fffffffe7fffffff  ffffffffffffffff 
00007f49ae8c6ab0:  ffffffffffffffff  ffffffffffffffff 
00007f49ae8c6ac0:  ffffffffffffffff  ffffffffffffffff 
00007f49ae8c6ad0:  ffffffffffffffff  ffffffffffffffff 
00007f49ae8c6ae0:  ffffffffffffffff  ffffffffffffffff 
00007f49ae8c6af0:  ffffffffffffffff  ffffffffffffffff 
00007f49ae8c6b00:  ffffffffffffffff  ffffffffffffffff 
00007f49ae8c6b10:  ffffffffffffffff  ffffffffffffffff 

goroutine 1 [syscall]:
runtime.cgocall(0x5e3ce0, 0xc000187e00, 0x9efbf8)
	/root/go/src/runtime/cgocall.go:154 +0x5b fp=0xc000187dd0 sp=0xc000187d98 pc=0x40c63b
github.com/robotn/gohook._Cfunc_stop_event(0x0)
	_cgo_gotypes.go:135 +0x45 fp=0xc000187e00 sp=0xc000187dd0 pc=0x4f5005
github.com/robotn/gohook.End()
	/root/go/pkg/mod/github.com/robotn/[email protected]/hook.go:247 +0x32 fp=0xc000187e30 sp=0xc000187e00 pc=0x4f62f2
github.com/go-vgo/robotgo.EventEnd(...)
	/root/go/pkg/mod/github.com/go-vgo/[email protected]/hook.go:36
main.main()
	/root/mmc-linux-src/switch/localserver/test.go:20 +0x20b fp=0xc000187f88 sp=0xc000187e30 pc=0x5d286b
runtime.main()
	/root/go/src/runtime/proc.go:225 +0x256 fp=0xc000187fe0 sp=0xc000187f88 pc=0x440db6
runtime.goexit()
	/root/go/src/runtime/asm_amd64.s:1371 +0x1 fp=0xc000187fe8 sp=0xc000187fe0 pc=0x473221

goroutine 18 [sleep]:
time.Sleep(0x2faf080)
	/root/go/src/runtime/time.go:193 +0xd2
github.com/robotn/gohook.Start.func1()
	/root/go/pkg/mod/github.com/robotn/[email protected]/hook.go:235 +0x32
created by github.com/robotn/gohook.Start
	/root/go/pkg/mod/github.com/robotn/[email protected]/hook.go:228 +0x86

rax    0x0
rbx    0x7f49ae8c7700
rcx    0x7f49d6e3a18b
rdx    0x0
rdi    0x2
rsi    0x7f49ae8c6a20
rbp    0x7f49d6faf588
rsp    0x7f49ae8c6a20
r8     0x0
r9     0x7f49ae8c6a20
r10    0x8
r11    0x246
r12    0x7f49d748e6c3
r13    0xa3
r14    0x7f49d748e6f5
r15    0x7f49a40139f0
rip    0x7f49d6e3a18b
rflags 0x246
cs     0x33
fs     0x0
gs     0x0

yuzhengjun1204 avatar Oct 12 '21 06:10 yuzhengjun1204

There is not need

for {
    EvChan := robotgo.EventStart()
}

because event start is event channel.

vcaesar avatar Oct 12 '21 14:10 vcaesar

Thank you for your answer. The above is the test code. I created a thread in C language and called robogo. EventStart() and robogo. EventEnd() many times, but when I called robogo. Eventend() several times, errors like the above will appear. Do you have any good solutions?

yuzhengjun1204 avatar Oct 13 '21 00:10 yuzhengjun1204

`IOHOOK_API int hook_stop() { int status = IOHOOK_FAILURE; if (hook != NULL && hook->ctrl.display != NULL && hook->ctrl.context != 0) { // We need to make sure the context is still valid. XRecordState *state = malloc(sizeof(XRecordState)); if (state != NULL) { if (XRecordGetContext(hook->ctrl.display, hook->ctrl.context, &state) != 0) { // Try to exit the thread naturally. if (state->enabled && XRecordDisableContext(hook->ctrl.display, hook->ctrl.context) != 0) { #ifdef USE_XRECORD_ASYNC pthread_mutex_lock(&hook_xrecord_mutex); running = false; pthread_cond_signal(&hook_xrecord_cond); pthread_mutex_unlock(&hook_xrecord_mutex); #endif

				// See Bug 42356 for more information.
				// https://bugs.freedesktop.org/show_bug.cgi?id=42356#c4
				//XFlush(hook->ctrl.display);
				XSync(hook->ctrl.display, False);
				if (hook->ctrl.display) {
					XCloseDisplay(hook->ctrl.display);
					hook->ctrl.display = NULL;
				}

				status = IOHOOK_SUCCESS;
			}
		} else {
			logger(LOG_LEVEL_ERROR,	"%s [%u]: XRecordGetContext failure!\n",
					__FUNCTION__, __LINE__);

			status = IOHOOK_ERROR_X_RECORD_GET_CONTEXT;
		}

		free(state);
	} else {
		logger(LOG_LEVEL_ERROR,	"%s [%u]: Failed to allocate memory for XRecordState!\n",
				__FUNCTION__, __LINE__);

		status = IOHOOK_ERROR_OUT_OF_MEMORY;
	}

	return status;
}


logger(LOG_LEVEL_DEBUG,	"%s [%u]: Status: %#X.\n",
		__FUNCTION__, __LINE__, status);
		


return status;

}`

It always breaks in here. // See Bug 42356 for more information. // https://bugs.freedesktop.org/show_bug.cgi?id=42356#c4 //XFlush(hook->ctrl.display); XSync(hook->ctrl.display, False); if (hook->ctrl.display) { XCloseDisplay(hook->ctrl.display); hook->ctrl.display = NULL; } what should I do to prevent breaking when i called start() and end() many times.

yuzhengjun1204 avatar Oct 13 '21 01:10 yuzhengjun1204

@vcaesar

yuzhengjun1204 avatar Oct 14 '21 09:10 yuzhengjun1204

Emm, I will test it on Ubuntu, this is X11 threaded bug. It work good on the macos.

vcaesar avatar Oct 14 '21 19:10 vcaesar