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

在1.2.12版本 获取锁仍然还是存在bug #243

Open
enddeadroyal opened this issue Nov 16, 2023 · 3 comments
Open

在1.2.12版本 获取锁仍然还是存在bug #243

enddeadroyal opened this issue Nov 16, 2023 · 3 comments

Comments

@enddeadroyal
Copy link

enddeadroyal commented Nov 16, 2023

在rotater.c文件中zlog_rotater_trylock函数中

static int zlog_rotater_trylock(zlog_rotater_t *a_rotater)
{
	int rc;

	rc = pthread_mutex_trylock(&(a_rotater->lock_mutex));
	if (rc == EBUSY) {
		zc_warn("pthread_mutex_trylock fail, as lock_mutex is locked by other threads");
		return -1;
	} else if (rc != 0) {
		zc_error("pthread_mutex_trylock fail, rc[%d]", rc);
		return -1;
	}

    a_rotater->lock_fd = lock_file(a_rotater->lock_file);
	if (a_rotater->lock_fd == INVALID_LOCK_FD) {
		return -1;
	}

	return 0;
}

lock_file失败的场合 并没有释放互斥量,导致后面程序trylock一直处于busy状态。
调用者 此处返回0,并没有跳转到exit段标,进行解锁释放。

int zlog_rotater_rotate(zlog_rotater_t *a_rotater,
   	char *base_path, size_t msg_len,
   	char *archive_path, long archive_max_size, int archive_max_count)
{
   int rc = 0;
   struct zlog_stat info;

   zc_assert(base_path, -1);

   if (zlog_rotater_trylock(a_rotater)) {
   	zc_warn("zlog_rotater_trylock fail, maybe lock by other process or threads");
   	return 0;
   }

   if (stat(base_path, &info)) {
   	rc = -1;
   	zc_error("stat [%s] fail, errno[%d]", base_path, errno);
   	goto exit;
   }

   if (info.st_size + msg_len <= archive_max_size) {
   	/* file not so big,
   	 * may alread rotate by oth process or thread,
   	 * return */
   	rc = 0;
   	goto exit;
   }

   /* begin list and move files */
   rc = zlog_rotater_lsmv(a_rotater, base_path, archive_path, archive_max_count);
   if (rc) {
   	zc_error("zlog_rotater_lsmv [%s] fail, return", base_path);
   	rc = -1;
   } /* else if (rc == 0) */

   //zc_debug("zlog_rotater_file_ls_mv success");

exit:
   /* unlock file */
   if (zlog_rotater_unlock(a_rotater)) {
   	zc_error("zlog_rotater_unlock fail");
   }

   return rc;
}

zlog_rotater_trylock函数应该修改为

if (a_rotater->lock_fd == INVALID_LOCK_FD) {
   	pthread_mutex_unlock(&(a_rotater->lock_mutex)); //添加释放互斥量
   	return -1;
}

@HardySimpson

@zhouyuyu888
Copy link

您说的地方改过了,还是2千多次锁在这:
#0 __lll_lock_wait () at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:135
#1 0x00007f7d2580ba24 in pthread_rwlock_rdlock () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_rwlock_rdlock.S:122
#2 0x00007f7d25c12047 in zlog (category=0x11b0cd0, file=0x401b73 "taskc.c", filelen=7, func=0x401c7a <FUNCTION.4360> "do_sig_child", funclen=12, line=189, level=30, format=0x401b80 "do_sig_child, pid=%d, tid=%d,sigs=%d\n") at zlog.c:1010
#3 0x00000000004013c6 in do_sig_child ()
#4
#5 pthread_rwlock_rdlock () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_rwlock_rdlock.S:47
#6 0x00007f7d25c12047 in zlog (category=0x11b0cd0, file=0x401b73 "taskc.c", filelen=7, func=0x401c87 <FUNCTION.4386> "main", funclen=4, line=323, level=30, format=0x401c39 "Parent ID %d\n") at zlog.c:1010
#7 0x0000000000401855 in main ()

@enddeadroyal
Copy link
Author

他那个文件锁是日志配置文件锁
不是日志本身的文件锁
@zhouyuyu888
目前我已经在商用环境使用
没有出现问题

@zhouyuyu888
Copy link

@enddeadroyal,zlog在一般场景中已经是非常安全了,我专门写的每秒并发50压力测试程序,的确都压出问题了,而且昨天试了一下,不轮转也有问题,日志文件写到58M就死锁了。

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