pread & related hang when I try to read /proc/pid/mem of rpcs3

Already posted this here:
https://cboard.cprogramming.com/c-programming/178765-pread-related-hang-when-i-try-read-proc-pid-mem-rpcs3.html
Now posting here as they rasied the good point that I should've stuck it in the linux forums.

I've tried countless times to get it to stop hanging by myself and now I'm done banging my head against the wall, on manjaro cinnamon x64 minimal, using kernel 5.4 LTS. The code I'm using is as follows:

proc_handle_t* proc_handle_open( int *err, int pid )
{
	proc_handle_t *handle;
#ifdef gasp_pread
	char path[32] = {0};
#endif
	
	if ( pid <= 0 ) {
		errno = EINVAL;
		if ( err ) *err = errno;
		ERRMSG( errno, "Invalid Pid");
		return NULL;
	}

	if ( !(handle = calloc(sizeof(proc_handle_t),1)) ) {
		if ( err ) *err = errno;
		ERRMSG( errno, "Couldn't allocate memory for handle");
		return NULL;
	}
	handle->perm = 0;
	handle->memfd = -1;
	
	if ( !proc_notice_info( err, pid, &(handle->notice) ) ) {
		proc_handle_shut( handle );
		return NULL;
	}
	
	if ( handle->notice.self )
		sprintf( handle->procdir, "/proc/self" );
	else {
		sprintf( handle->procdir, "/proc/%d", pid );
#ifdef PTRACE_SIEZE
		ptrace( PTRACE_SEIZE, pid, NULL, NULL );
#endif
#ifdef gasp_pread
		sprintf( path, "/proc/%d/mem", pid );
		handle->memfd = open( path, O_RDWR );
		if ( handle->memfd >= 0 )
			handle->perm = O_RDWR;
		else {
			handle->memfd = open( path, O_RDONLY );
			if ( handle->memfd >= 0 )
				handle->perm = 0;
		}
#endif
	}
	
	handle->running = 1;
	
	return handle;
}
...
intptr_t proc__glance_file(
	int *err, proc_handle_t *handle,
	intptr_t addr, void *mem, size_t size
)
{
#ifdef gasp_pread
	intptr_t done = 0;
	int ret;
	
	if ( (ret = proc__rwvmem_test(handle, mem,size)) != EXIT_SUCCESS )
	{
		if ( err ) *err = ret;
		return 0;
	}
	
	if ( (handle->perm != O_RDWR && handle->perm != O_RDONLY)
		|| handle->memfd < 0 )
		return 0;
	
	done = gasp_pread( handle->memfd, mem, size, addr );
	if ( done < 0 ) {
		if ( err ) *err = errno;
		ERRMSG( errno, "Couldn't read process memory" );
	}
	return done;
#else
	if ( err ) err = ENOSYS;
	return 0;
#endif
}
...
intptr_t proc_glance_data(
	int *err, proc_handle_t *handle,
	intptr_t addr, void *mem, size_t size
)
{
	intptr_t done;
	mode_t perm;
	int ret = proc__rwvmem_test( handle, mem, size );
	
	if ( ret != EXIT_SUCCESS ) {
		if ( err ) *err = ret;
		return 0;
	}
	errno = EXIT_SUCCESS;

#ifndef PTRACE_SEIZE
	ptrace( PTRACE_ATTACH, handle->notice.entryId, NULL, NULL );
#else
	ptrace( PTRACE_INTERRUPT, handle->notice.entryId, NULL, NULL );
#endif
	
	if ( (perm = proc_mapped_addr( &ret, handle, addr, 0777, NULL )) == 0
		&& ret != EXIT_SUCCESS ) {
		if ( err ) *err = ret;

		ptrace( PTRACE_CONT, handle->notice.entryId, NULL, NULL );
		return 0;
	}
	
	/* Aviod fiddling with own memory file */
	if ( (done = proc__glance_self( err, handle, addr, mem, size ))
		> 0 ) goto success;
	/* gasp_pread() */
	if ( (done = proc__glance_file( err, handle, addr, mem, size ))
		> 0 ) goto success;
	/* gasp_lseek() & gasp_read() */
	if ( (done = proc__glance_seek( err, handle, addr, mem, size ))
		> 0 ) goto success;
	/* Last ditch effort to avoid ptrace */
	if ( (done = proc__glance_vmem( err, handle, addr, mem, size ))
		> 0 ) goto success;
	/* ptrace */
	if ( (done = proc__glance_peek( err, handle, addr, mem, size ))
		> 0 ) goto success;
	
#ifndef PTRACE_SEIZE
	ptrace( PTRACE_DETACH, handle->notice.entryId, NULL, NULL );
#else
	ptrace( PTRACE_CONT, handle->notice.entryId, NULL, NULL );
#endif

	if ( proc_mapped_addr( &ret, handle, addr, perm, NULL ) == 0
		&& ret != EXIT_SUCCESS && err )
		*err = ret;
	return 0;
	success:

#ifndef PTRACE_SEIZE
	ptrace( PTRACE_DETACH, handle->notice.entryId, NULL, NULL );
#else
	ptrace( PTRACE_CONT, handle->notice.entryId, NULL, NULL );
#endif

	if ( proc_mapped_addr( &ret, handle, addr, perm, NULL ) == 0
		&& ret != EXIT_SUCCESS && err )
		*err = ret;
	return done;
}

The gasp_* is just a define that changes based on whether large file support is detect at compile time or not, in this case it is and maps to pread64 etc
Full project code here:
GitHub - awsdert/gasp at 6e028dcbc6853b469dfd63ead6fae82e42079649

Managed to solve it by delaying the attach until right before I attempt to read/write the memory, also had to stick in a kill( pid, SIGSTOP ); before the attempt to attach

Forum kindly sponsored by